Q. Use of array in assembly language?
Let's write a program to add two 5-byte numbers stored in an array. For illustration two numbers in hex can be:
Let's also presume that numbers are represented as the lowest significant byte first and put in memory in two arrays. The result is stored in third array SUM. SUM also includes the carry out information therefore would be 1 byte longer than number arrays.
; Program: Add two five-byte numbers using arrays
; ALGORITHM:
; Make count = LEN
; Clear the carry flag
; Load address of NUM1
; REPEAT
; Put byte from NUM1 in accumulator
; Add byte from NUM2 to accumulator + carry
; Store result in SUM
; Decrement count
; Increment to next address
; UNTIL count = 0
; Rotate carry into LSB of accumulator
; Mask all but LSB of accumulator
; Store carry result, address pointer in correct position.
; PORTS : None used
; PROCEDURES: None used
; REGISTERS: Uses CS, DS, AX, CX, BX, DX
DATA SEGMENT
NUM1 DB 0FFh, 10h ,01h ,11h ,20h
NUM2 DB 10h, 20h, 30h, 40h ,0FFh
SUM DB 6DUP (0)
DATA ENDS
LEN EQU 05h; constant for length of the array
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA; initialise data segment
MOV DS, AX; using AX register
MOV SI, 00; load displacement of 1stnumber.
; SI is being used as index register
MOV CX, 0000; clear counter
MOV CL, LEN; set up count to designed length
CLC ; clear carry. Ready for addition
AGAIN: MOV AL, NUM1 [SI]; get a byte from NUM1
ADC AL, NUM2 [SI]; add to byte from NUM2 with carry
MOV SUM [SI], AL; store in SUM array
INC SI
LOOP AGAIN; continue until no more bytes
RCL AL, 01h; move carry into bit 0 of AL
AND AL, 01h; mask all but the 0th bit of AL
MOV SUM [SI], AL; put carry into 6th byte
FINISH: MOV AX, 4C00h
INT 21h
CODE ENDS
END START