【问题标题】:In my assembly code , the subroutine returned ,but the code started again from begining [duplicate]在我的汇编代码中,子程序返回了,但代码又从头开始[重复]
【发布时间】:2021-08-29 04:05:03
【问题描述】:

代码在这里,我的电脑是x86。这段代码的功能是十六进制到十进制的转换,但是还没有完成。现在它只是将数字放入堆栈。

ASSUME cs:code,ds:data
data SEGMENT
ENDS

code SEGMENT
start:
    mov ax,data
    mov ds,ax
    mov bx,19 ;the number needed to be output as decimal format is saved in bx

    call output

    mov ah,4CH  
    int 21H 

output proc
        mov ax,bx ;the number that needed to be output is saved in bx , now move it to ax
        mov bx,10
        mov cx,0 ;use cx to record how many figure needed to be output
    division:  
        mov dx,0
        div bx
        push dx; save the remainder in stack 
        inc cx
        cmp ax,0
        jne division  ;if the ax is not zero,means still need ax still need to be divided
        ret 
output endp

code ENDS
end start

当我直接运行代码时,窗口无法正常关闭。 当我调试代码时,子程序返回,但是代码又从头开始:(

【问题讨论】:

  • push dx; save the remainder in stack - 那是ret 返回的地方。
  • 在执行output函数的ret之前,需要清理堆栈,使ss:sp再次指向返回地址。您必须运行许多 pop 指令才能实现此目的。
  • 非常感谢,完美解决问题。

标签: assembly x86-16


【解决方案1】:

必须清理堆栈,否则 ret 无法正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    相关资源
    最近更新 更多