我正在编写一个 ASM 程序,它将两个数字相除并计算 20 位小数。我的策略是用长除法计算下一个数字并将它们推入堆栈。然后获取堆栈指针,从中减去 20 位 * 8 位并从该地址写入数字,然后将 8 添加到该地址,写入该数字等。但是当我尝试它只写出零时。数字在堆栈上,因为我尝试只是“弹出”它们并且效果很好。我只需要按照它们被推动的顺序弹出它们,而不是颠倒。
我的代码:
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