0

code is here, my computer is x86.The function of this code is the transfer Hexadecimal to Decimal , but haved be finished yet .Now it just put the figer in stack.

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

When I directed run the code ,the window can't be closed normally. When I debug the code ,the subroutine returned ,However the code start from the begining again :( enter image description here

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 3
    `push dx; save the remainder in stack` - that is where the `ret` is returning to. – Raymond Chen Jun 12 '21 at 15:18
  • 1
    Before executing the `ret` of the `output` function you need to clean up the stack so that `ss:sp` points at the return address again. You will have to run a number of `pop` instructions to achieve this. – ecm Jun 12 '21 at 15:22
  • Thanks so much ,fix the problem perfectly. – Fisherman_Zhu Jun 12 '21 at 15:38

1 Answers1

1

The stack have to be cleaned ,otherwise the ret can't work in the proper way.