簡體   English   中英

程序集8086循環問題

[英]Assembly 8086 loops issue

偽代碼如下:

read c        //a double digit number
for(i=1,n,i++)
{ if (n%i==0)
     print i;}

在匯編中我把它寫成:

mov bx,ax   ;  ax was the number  ex.0020, storing a copy in bx.

mov cx,1    ; the start of the for loop
.forloop:
mov ax,bx   ; resetting ax to be the number(needed for the next iterations)
div cx
cmp ah,0    ; checking if the remainder is 0 
jne .ifinstr
add cl 48    ;adding so my number would be displayed later as decimal
mov dl,cl   ;printing the remainder
mov ah,2
int 21h
sub cl,48   ;converting it back to hexa
.ifinstr:
inc cx      ;the loop goes on
cmp cx,bx
jle .forloop

我已經通過追蹤它的步驟進行了檢查。 第一次迭代順利進行,然后,在第二次迭代中,它使ax =初始數字和cx = 2,但是在'div cx'它會跳到我不知道的地方並且它不會停在任何地方。 它確實:

push ax
mov al,12
nop
push 9
.
.

知道為什么會這樣嗎?

嘗試在div指令之前執行mov dx,0。 基本上每次跳轉后,dx寄存器中都可能有一些數據,所以你可以在dx或XOR dx,dx中移動零。 這是要做的,因為否則划分將被視為不同。 見:無符號除法。

算法:

when operand is a byte:
AL = AX / operand
AH = remainder (modulus) 

when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus) 

例:

MOV AX,203; AX = 00CBh MOV BL,4 DIV BL; AL = 50(32h),AH = 3 RET

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM