Write a NEAR subroutine using 8086 assembly Language (with proper comments) that returns the smallest byte value in a byte array of length 5-bytes. The array is declared in the calling program and the base address of the array is passed to the subroutine in the stack. You should write both the calling program and subroutine.
; 8086 assembly Language
; DosBox
org 100h
.model small
.stack 200h
.data
array DB 10,3,2,5,9 ; a byte array of length 5-bytes
.code
start:
mov ax,@data ;set segment DATA
mov ds,ax
mov es,ax
; the base address of the array is passed
; to the subroutine in the stack
mov dx, offset array
PUSH DX
CALL My_PROC
NOP
mov ah, 4ch
int 21h ; exit
;************************************************
; PROC My_PROC
; Input: array address in stack
; Output: AL Smallest byte value
;************************************************
My_PROC PROC NEAR
ADD SP, 2
POP SI ; array address
MOV AL, [SI] ; Smallest = array[0]
MOV CX, 4 ; counter
L1:
INC SI ; address of next byte
MOV AH, [SI] ; AH = array[i]
CMP AH,AL ; compare Smallest and array[i]
JNC NEXT ; IF array[i] > Smallest goto NEXT
MOV AL, AH ; ELSE AL = new Smallest
NEXT:
LOOP L1 ; next loop
PUSH AX ; AL = Smallest
SUB SP, 2 ; restore stack
END_PROC:
RET
My_PROC ENDP
end start
Comments
Leave a comment