I am writing an ASM program that divides two numbers and calculates 20 decimal places. My strategy was to calculate the next digits with a long division sort of process and push them to the stack. Then get the stack pointer, subtract 20 digits * 8 bits from it and write the digit from that address, then add 8 to that adress, write that digit etc. But when I try that it writes out only zeros. The digits are on the stack because I tried just "popping" them and that works fine. I just need them to be popped in the same order that they were pushed, not reversed.
my code:
dane SEGMENT ;segment danych
text db 0dh, 0ah,"Give b (a/b)", 0dh, 0ah, "$"
text2 db 0dh, 0ah,"Give a (a/b)", 0dh, 0ah, "$"
text3 db 0dh, 0ah,"a/b:", 0dh, 0ah, "$"
quot db 0h
rem db 0h
counter db 0h
dane ENDS
; C:\ml /Fl /Zm /Zi /c lab3.asm
; E:\link /CODEVIEW lab3.obj
rozkazy SEGMENT 'CODE' use16 ;segment rozkazu
ASSUME cs:rozkazy, ds:dane
start:
mov ax, SEG dane
mov ds, ax
mov cl, 03h
mov ch, 0Ah
mov dx, offset text
mov ah, 09h
int 21h ; "input divisor"
mov ah, 01h
int 21h ; input into al
mov cl, al
sub cl, 30h ; move to cl and subtract 30h to get value instead of ascii
mov dx, offset text2
mov ah, 09h
int 21h ; "input dividend"
mov ah, 01h
int 21h
sub al, 30h ; input into al and get number instead of ascii
jmp divide
divide:
cbw
div cl ; convert al to ax and divide by cl
mov dl, ah
cbw ; convert al -> ax
push ax ; push ax to stack
mov al, dl ; move remainder from division to al
xor ah, ah
cbw
mul ch ; clear ah and al - > ax, multiply remainder times 10
inc counter ; increase counter by 1
cmp counter, 14h ; if the division was preformed 20 times, jump to show
jz show
jmp divide
show:
mov dx, offset text3
mov ah, 09h
int 21h ; "your number"
mov bx, sp
sub bx, 160 ; get stack pointer address and subtract by 8*20 (to get address of the number that is 20 positions down from sp)
jmp show2
show2:
mov dx, [bx] ; move value to dx from address that is stored in bx
add dl, 30h ; add 30h to get ascii
mov ah, 02h
int 21h ; write dl out
dec counter ; decrease counter
add bx, 8h ; move our memory pointer 8 up (to next number)
cmp counter, 0h
jz finish ; if counter = 0 jump to finish
jmp show2
finish:
mov ah, 4CH
int 21H
rozkazy ENDS
stosik SEGMENT stack
dw 128 dup(?)
stosik ENDS
END start