***********************************************************************************************************
AUTHOR: ANUEBUNWA VICTOR OGECHUKWU
SUBROUTINE TO CONVERT CHARACTERS IN MEMORY TO A DECIMAL VALUE
Subroutine CHAR2INTR converts characters in memory to a decimal value and stores it in AX.
i.e '1','2','3','4' in memory ==> 1234 in AX
.......................................................................HOW TO USE......................................................................
(1) Load the starting address of the memory into SI register.
(2) Call CHAR2INTR. ***********************************************************************************************************
; SAMPLE PROGRAM
.MODEL SMALL
.STACK
.DATA
AVON DB ?
.CODE
START: MOV AX, @DATA
MOV DS, AX
MOV AH, 3FH
LEA DX, AVON
INT 21H ;Reads characters into memory, using AVON as a starting address
LEA SI, AVON ;Loads starting address into SI
CALL CHAR2INTR ;Calls CHAR2INTR to convert characters to a decimal value
MOV DX, AX ;Puts decimal value into DX
MOV AH, 02H
INT 21H ;Displays character whose ASCII code equals the decimal value
MOV AH, 4CH
INT 21H
;Subroutine CHAR2INTR
CHAR2INTR PROC
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSHF
MOV AX, 0000H
MOV CX, 0000H
CLOOP1: MOV AX, [SI] ;Loads character currently pointed to into AX
CMP AL, '9'
JNLE XCLOOP1 ;Exits loop if character is not less or equal to '9'
CMP AL, '0'
JNGE XCLOOP1 ;Exits loop if character is not greater or equal to '0'
INC CL ;Else, Keeps count of the number of characters being processed
INC SI ;Moves pointer to the next character
SUB AL, 30H ;Converts character to digit
PUSH AX ;Pushes digit to stack
JMP CLOOP1 ;continues loop
XCLOOP1: MOV BX, 0001H
MOV DX, 0000H
CLOOP2: CMP CL, 0 ;Checks number of characters processed
JE XCLOOP2 ;Exits loop if no character was processed i.e CL = 0
POP AX ;Else, pops digit from stack to AX
MUL BL ;Multiplies digit with it's PLACEMENT-VALUE. RECALL: unit = 1, tens = 10, hundreds = 100 ...
ADD DX, AX ;Adds (digit * PLACEMENT-VALUE)s together. RECALL: 234 = (4 * 1) + (3 * 10) + (2 * 100) where 1,10,100 represent their PLACEMENT-VALUES
MOV AX, 10
MUL BL ;Multiplies current PLACEMENT-VALUE by 10 to advance to the next PLACEMENT-VALUE.
XCHG AX, BX ;Puts result from multiplication back to BX
DEC CX ;Reduces count
JMP CLOOP2 ;Loop continues
XCLOOP2: MOV AX, DX ;Moves converted value to AX
POPF
POP SI
POP DX
POP CX
POP BX
RET
CHAR2INTR ENDP
;End of Subroutine CHAR2INTR
END START
view in pastebin
No comments:
Post a Comment