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...
ASM 16 bit STACK TUTORIAL
Started by JMC31337, Feb 15 2009 05:59 PM
3 replies to this topic
#1
Posted 15 February 2009 - 05:59 PM
"Your Life Is Your Crime, It's Punishment Time"
|
|
|
#2
Posted 16 February 2009 - 10:30 AM
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?
#3
Posted 16 February 2009 - 06:25 PM
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.
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
Posted 17 February 2009 - 11:52 AM
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.
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"


Sign In
Create Account


Back to top









