Monday 4 March 2013

PROCEDURE TO COLLECT MULTIPLE DIGITS AS INPUT IN 8086 ASSEMBLY


;PROCEDURE TO COLLECT MULTIPLE DIGITS AS INPUT IN 8086 ASSEMBLY
;AUTHOR: ANUEBUNWA VICTOR O.
;ASSEMBLER: MASM611

;You should have the following declared in your data segment.
value DB 0000H
e1 DB "Wrong input",'$'

e2 DB 'No value entered',0AH,0DH,'Program exiting...','$'


;You may avoid using PUSHA and POPA if it gives error during assembling but be sure to backup needed values in AX,BX,CX and DX before calling this procedure.

;Beginning of procedure
read proc
PUSHA                           ;Push current values in register to stack
MOV AX, 0000H ;
MOV CX, 0000H ;
MOV BX, 0000H ;
MOV DX, 0000H ;Clears register AX,BX,CX and DX
.tryloop1:         MOV AH, 01H
INT 21H ;Calls DOS funtion to receive a character
JMP check ;Jumps to check character recieved
continue_loop: ;Where loop continues assuming character passes check
MOV BX, 0000H ;Clears BX again
MOV BL, AL ;Moves AL to BL
SUB BX, 30H ;Subtracts 30H to get the actual value of character
PUSH BX ;Push BX to stack
INC CX ;Increments CX to track the lenght of the value
CMP AL, 0DH ;Checks if character was carriage return
JNE .tryloop1 ;Jumps if not carrige return

CMP CX, 1 ;Checks if no value was entered
JE error_msg2 ;jumps if no value was entered
POP AX ;Pops out last value in stack(carrige return)
dec CX ;Decrements counter

MOV BX, 1 ;initialize multiplier
.tryloop2:          MOV AX, 0000H ;Clears AX
POP AX ;Pops data to AX
MUL BX ;Multiplies AX by BX
ADD value, AX ;Add AX to value

XCHG AX, BX ;Exchanges AX and BX
MOV BX, 10 ;Moves 10 into BX
MUL BX ;Multiplies AX by BX(containing 10)
XCHG AX, BX ;Exchanges back AX and BX
dec CX ;Decrements counter
JNE .tryloop2 ;Repeats until CX equals zero

MOV AH, 02H
MOV DL, 0aH
INT 21H ;Prints line-feed
POPA                      ;Pops values of register before procedure call from stack
RET ;returns control
read endp                                          ;End of procedure.

check: CMP AL, 0DH ;Checks if the character is carriage return
JE continue_loop ;Jumps back into loop if carriage return
CMP AL, '0' ;Checks if a non-digit was entered
JNGE error_msg
CMP AL, '9'
JNLE error_msg ;Displays error if a non-digit was entered
JMP continue_loop ;Else it jumps back into loop

error_msg: MOV AH, 02H
MOV DL, 0AH
INT 21H ;Prints line-feed
LEA DX, e1
MOV AH, 09H
INT 21H ;Prints string
JMP start ;Restarts program(assuming you have Start: as your starting label)


error_msg2: MOV AH, 02H
MOV DL, 0AH
INT 21H ;Prints line-feed
LEA DX, e2
MOV AH, 09H
INT 21H ;Prints string
JMP stp ;Stops program


No comments: