.data
temp_read dw ?
backup_ax dw ?
backup_bx dw ?
backup_dx dw ?
.code
SCAN_MULTIPLE_DIGITS:
call BACKUP_REG
mov temp_read, 0
READCHAR:
mov ax, temp_read
mov bx, 10
mov dx, 0
mul bx
mov temp_read, ax
mov ax, 0
mov ah, 01h
int 21h
cmp al, 13
je ENDREADCHAR
sub al, '0'
mov bx, 0
mov bl, al
add temp_read, bx
jmp READCHAR
ENDREADCHAR:
mov ax, temp_read
mov bx, 10
div bx
mov bx, backup_bx
mov dx, backup_dx
ret
; Printing multiple digits
; Input: dx
; Output: none
; Example:
; mov dx, 322
; call PRINT_MULTIPLE_DIGITS
; **Max printing value --> 65535 (size of dx = 16 bits / 2 bytes )
SHOW_MULTIPLE_DIGITS:
call BACKUP_REG
mov ax, "$"
push ax
mov ax, dx
mov bx, 10
mov dx, 0
EXTRACTCHAR:
div bx
add dx, '0'
push dx
mov dx, 0
cmp ax, 0
je FINISH
jmp EXTRACTCHAR
FINISH:
PRINT:
pop dx
cmp dx, "$"
je ENDPRINT
mov ah,02h
int 21h
jmp PRINT
ENDPRINT:
call RESTORE_REG
ret
BACKUP_REG:
mov backup_ax, ax
mov backup_bx, bx
mov backup_dx, dx
ret
RESTORE_REG:
mov ax, backup_ax
mov bx, backup_bx
mov dx, backup_dx
ret
Asked
Active
Viewed 28 times
0

vitsoft
- 5,515
- 1
- 18
- 31
-
These are just two utility functions - there's no main logic. – 500 - Internal Server Error Sep 10 '21 at 15:27
-
1Don't waste your time on insane code like this. Look at https://stackoverflow.com/questions/53801552/assembly-x86-priting-a-number – vitsoft Sep 10 '21 at 15:32
-
what is an utility code ? – Chin Ka Hou Sep 10 '21 at 16:15
-
1Something that isn't central to the overall purpose of the program. There's nothing here that actually calls `SCAN_MULTIPLE_DIGITS` or `SHOW_MULTIPLE_DIGITS`. – 500 - Internal Server Error Sep 10 '21 at 16:20
-
1[Displaying numbers with DOS](https://stackoverflow.com/q/45904075) explains why repeated division by 10 works, including a version with push and pop loops to get printing order. (Instead of just storing in reverse order into a buffer). – Peter Cordes Sep 11 '21 at 00:36