tj94
tj94

Reputation: 61

Stack in Assembler under MS-DOS

I am trying to learn some Assembler code; specifically how to use the stack and how it works, etc. However, I run into an error when I use the code below:

entry:
    push 0x42
    call teststack
    jmp hang

hang:
    jmp hang

teststack:
    mov ah, 0x0e
    pop al
    mov bh, 0x00
    mov bl, 0x04
    mov cx, 0x01
    int 10h
    ret

What I am trying to do is pass the number 42 (life, universe, everything :D) into my teststack procedure, where it will print Ascii 0x42 (I think capital B). My issue is a compiler error, for the line that reads pop al:

**error: invalid combination of opcode and operands**

I am using NASM inside a Windows 98 virtual machine. Any help with my error would be greatly appreciated.

Upvotes: 0

Views: 738

Answers (3)

marekb
marekb

Reputation: 141

If you push some value on stack (i.e. 0x42 value) your stack will contain one value [0x42]. Next you use call instruction which stores the return address on stack, so stack contans now [ret_addr, 0x42].

First sub-routine instruction pops value (unexpectedly it is ret-addr and not 0x42), then does other stuff and jumps (ret instruction) into address poped from stack (0x42...).

You could use copy of stack pointer in BP register to read value stored on stack and return from sub-routine using RET N instruction (higher level languages uses similar sheme).

Upvotes: 1

Antonio Bakula
Antonio Bakula

Reputation: 20693

Didn't ever use NASM but I presume that you are in 16 bit mode and pop is by default using 16 bit registers, so you can't use ah. When you pushed 0x42 to stack that was a 16 bit value (0x0042). It's safe to use pop ax and then set ah :

entry:
    push 0x42
    call teststack
    jmp hang

hang:
    jmp hang

teststack:
    pop ax           ; ax = 0x0042
    mov ah, 0x0e     ; ax = 0x0e42
    mov bh, 0x00
    mov bl, 0x04
    mov cx, 0x01
    int 10h
ret

Upvotes: 0

Jakub Oboza
Jakub Oboza

Reputation: 5421

with push you have to specify size like this

push dword 0x42

Upvotes: 0

Related Questions