Jump to content

ASM 16 bit STACK TUTORIAL

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
JMC31337

JMC31337

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
this is a NASM related Assembly tutorial explaining how to utilize stacks. A key thing to remember is LIFO (Last In First Out) as i will explain in the code
compile with nasm -f bin test.asm -o test.com
also i dont use code tags so my apologies up front
[BITS 16]
[ORG 100h]
[SECTION .text]
start:
mov dx,msg1
mov ah,9h ;DISPLAY STRING TEST 1
int 21h

mov dx,msg2
mov ah,9h ;DISPLAY STRING TEST 2
int 21h

mov dx,msg3 ;MOVE STACK TEST 1 into data register
push dx ;push onto STACK

mov dx,msg4 ;MOVE STACK TEST 2 into data register
push dx ;push onto STACK

pop dx ;POP from stack to dx data register
;KEY PT!! we have STACK 1 then STACK 2 on
; the stack so we call it in reverse STACK 2
;then STACK 1
mov ah,9h ;DISPLAY IT
int 21h

pop dx ;pop stack 1
mov ah,9h ;display it!!
int 21h

mov ax,4c00h
int 21h
[SECTION .data]
msg1 db 10,13,"String TEST 1","$"
msg2 db 10,13,"String TEST 2","$"
msg3 db 10,13,"STACK 1","$"
msg4 db 10,13,"STACK 2","$"

another thing that is common is CLEARING your registers, which can be done via XOR ax,ax dx,dx etc...
also you can push values into an accumulator register AX then pop on the stack, mov dx,ax mov from ax TO dx then display...short simple to the pt...
"Your Life Is Your Crime, It's Punishment Time"

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Out of curiosity, do the items on the stack have to be strings, or can the be other types of data? Also, can you mix data items?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
The stack consists entirely of nothing but numbers, either values or addresses...
The only two functions in that example that modify the stack are push and pop.

He's pushing and popping the address of the two strings, then using an os-dependant interrupt call to 0x21 to print the string.

Also...don't know what you meant by clearing the registers (which xor does do) but on anything other then DOS you don't actually have to, nor do you need to before you do a mov, add, div, ect..., because they aren't cumulative.

One thing to be careful of when mixing C and Asm is stack corruption, because parameters to a function are passed on the stack. If you push anything, always make sure to pop it before leaving the asm statement.

#4
JMC31337

JMC31337

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts

TkTech said:

The stack consists entirely of nothing but numbers, either values or addresses...
The only two functions in that example that modify the stack are push and pop.

He's pushing and popping the address of the two strings, then using an os-dependant interrupt call to 0x21 to print the string.

Also...don't know what you meant by clearing the registers (which xor does do) but on anything other then DOS you don't actually have to, nor do you need to before you do a mov, add, div, ect..., because they aren't cumulative.

One thing to be careful of when mixing C and Asm is stack corruption, because parameters to a function are passed on the stack. If you push anything, always make sure to pop it before leaving the asm statement.

thnx for the clearing registers info....this will be useful for Linux's nasm....
"Your Life Is Your Crime, It's Punishment Time"