Search code examples
assemblyx86-16emu8086

Assembly 8086 - Can't push two symbols


This program converts binary to decimal and I'm supposed to push "$$" into a stack, so that later it would recognize "$$" and end the program. However, it gives me an error that "$$" is an invalid parameter for PUSH.

Everything else works for this program, I just need to somehow mark the end in the stack without an error. I have tried putting in numbers such as "00" and it creates an infinite loop instead of an error. I'm using emu8086, if that's the case.

PUSH    ax
PUSH    cx
PUSH    dx

MOV cx, 10  
PUSH    "$$"    ; pushes $$ to mark the end
Divide:
MOV dx, 0       
DIV cx      
PUSH    dx      
CMP ax, 0   
JA  Divide


MOV ah, 2
Print:
POP dx      
CMP dx, "$$"  ; compares if there's a mark, to end the program
JE  End
ADD dl, '0'     
INT 21h     
JMP Print   

Solution

  • An instruction like PUSH "$$" would normally not be legal for an emulator that is based on the 8086 architecture where it was not possible to push an immediate value like "$$".

    The workaround is to mov the value to a register and push the register:

    mov     dx, "$$"
    push    dx
    

    In your program specifically, you can push the CONST 10 (already present in CX), because no remainder will ever equal that value:

    MOV  cx, 10  
    PUSH CX    ; pushes 10 to mark the end
    Divide:
    MOV  dx, 0       
    DIV  cx      
    PUSH dx      
    CMP  ax, 0   
    JA   Divide
    
    
    MOV  ah, 2
    Print:
    POP  dx      
    CMP  dx, CX  ; compares if there's a mark, to end the program
    JE   End
    ADD  dl, '0'     
    INT  21h     
    JMP  Print