2

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   
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Paul
  • 53
  • 4
  • 2
    What assembler do you use and what are translation flags? `push imm16` available for 80186 but absent in 8086. – dimich Oct 06 '22 at 12:52

1 Answers1

3

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
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Is this $$ as in 0x2424 or $$ as in section start? I'm guessing the former. – puppydrum64 Dec 15 '22 at 14:15
  • @puppydrum64 Why would you guess? The OP already wrote for what purpose they want to use the value '$$' == 0x2424. It's a sentinel they put on the stack so that their second loop can detect when it's time to stop iterating. – Sep Roland Dec 20 '22 at 19:59
  • I didn't see that but I suppose the quotation marks around `"$$"` implies that it's ASCII – puppydrum64 Dec 21 '22 at 16:21