Here's the well tested code for printing number
.186
.model SMALL
.STACK 100h
.data
HelloMessage db 'Hello World', 13, 10, '$'
SuperNumber dw 13565
.code
; Writes word number from AX to output
; Usage
; .186
; ....
; mov ax, 13444
; call PRINT_NUMBER
PRINT_NUMBER PROC NEAR ; Outputs integer word number stored in AX registry. Requires CPU
; Save state of registers.
PUSHA ; Save all general purpose registers
PUSH BP ; We're going to change that.
; we need variables
; word number; 2 bytes
; byte digitsCount; 1 byte
; reserving space for variables on stack (3 bytes)
mov bp, sp; ; bp := address of the top of a stack
sub sp, 3*8 ; allocate 3 bytes on stack. Addresses of variables will be
; number: WORD PTR [rbp - 2*8]
; digitsCount: BYTE PTR [rbp - 3*8]
; Getting digits
; number = ax;
; digitsCount = 0;
; do
; {
; push number%10;
; digitsCount++;
; number = number / 10;
; }
; while (number > 0)
mov WORD PTR [bp - 2*8], ax ; number = ax;
mov BYTE PTR [bp - 3*8], 0 ; digitsCount = 0;
getDigits: ; do
mov ax, WORD PTR [bp - 2*8]; number/10: ax = number / 10, dx: number % 10
;cwd
mov dx, 0
mov bx, 10
div bx
push dx ; push number%10
mov WORD PTR[bp - 2*8], ax; number = number/10;
inc byte PTR[bp - 3*8] ; digitsCount++;
cmp WORD PTR[bp - 2*8], 0; compare number and 0
je getDigitsEnd ; if number == 0 goto getDigitsEnd
jmp getDigits ; goto getDigits;
getDigitsEnd:
mov ah, 9
mov dx, offset HelloMessage
int 21h
;while (digitsCount > 0)
;{
; pop digit into ax
; print digit
; digitsCount--;
;}
printDigits:
cmp BYTE PTR[bp - 3*8], 0; compare digits count and 0
je printDigitsEnd ; if digitsCount == 0 goto printDigitsEnd
pop ax ; pop digit into al
add al, 30h ; get character from digit into al
mov ah, 0eh ; wanna print digit!
int 10h ; BIOS, do it!
dec BYTE PTR[bp - 3*8] ; digitsCount--
jmp printDigits ; goto printDigits
printDigitsEnd:
; Deallocate local variables back.
mov sp, bp
; Restore state of registers in reverse order.
POP BP
POPA
; Exit from procedure.
RET
PRINT_NUMBER ENDP
start:
mov ax, @data
mov ds, ax
mov ah, 9
mov dx, offset HelloMessage
int 21h
mov ax, 64454
call Print_Number
mov ah, 4ch
int 21h
end start
DX
before dividing.DIV BX
dividesDX:AX
byBX
. – Telmatelo