Author Topic: Stack definition problem NASM-32bit-Linux  (Read 13648 times)

Offline tarek89

  • Jr. Member
  • *
  • Posts: 20
Stack definition problem NASM-32bit-Linux
« on: April 25, 2011, 09:29:37 PM »
Hello

I am writing intelx8086 32bit assembly programs on linux 64bit machine
the problem is i cant deal with stack

i used the declaration in NASM manual:

segment stack stack
 resb 64
stacktop:

it results with an error:
warning: Unknown section attribute 'stack' ignored on declaration of section `stack'
warning: uninitialized space declared in non-BSS section `stack': zeroing

i dont know what to do, i need to use the stack
here is a code for my stack checking program
Code: [Select]


section .data ;--------------Data Declaration-------------------

I_MSG DB 'Please Enter Number=',10 ;Input Msg for user
I_MSG_COUNT EQU $ - I_MSG ;Length of first message

segment stack stack
resb 64
stacktop:

section .text  ;---------------Code----------------------------

global _start

_start:

LEA ECX,[I_MSG] ;Moves offset of msg to EDX Register
MOV EDX,I_MSG_COUNT ;load the length of the stream to print
CALL _print
CALL _exit
;--------------------------Procedure _print-----------------
_print:
PUSHF
MOV EAX,4 ;sys_write
MOV EBX,1 ;file descriptor 1
INT 80h
RET
; Call Kernel
; Return

;---------------------------Procedure _exit-------------------
_exit: MOV EAX,1 ;sys_exit
MOV EBX,0 ;exit with no error
INT 80h
RET ;Call The Kernel
;Return






IF i removed PUSHF it works fine with no error

Assemble: nasm -f elf prog.asm
Link: ld -m elf_i386 -s -o prog prog.o


Thank you in advance

« Last Edit: April 25, 2011, 09:48:17 PM by tarek89 »

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Stack definition problem NASM-32bit-Linux
« Reply #1 on: April 25, 2011, 09:35:06 PM »
Code: [Select]
;--------------------------Procedure _print-----------------
_print:
PUSHF          ;<-- THIS OPERATION IS...
MOV EAX,4
MOV EBX,1
INT 80h
RET             ;<-- MESSING UP YOUR CALL STACK

Examine my comments within your source; Hopefully this will shed some light on why your stack gets hosed thus crashing your program...

Offline tarek89

  • Jr. Member
  • *
  • Posts: 20
Re: Stack definition problem NASM-32bit-Linux
« Reply #2 on: April 25, 2011, 09:39:10 PM »
Thank you alot i got it
but the reason is my main problem has a segmentation fault error which i dont know how to fix or even get where it is
so i made this small prog to test the push and pop issue

so how could i work on stack without even declaring it or reserving a size?

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Stack definition problem NASM-32bit-Linux
« Reply #3 on: April 25, 2011, 09:53:16 PM »

An extremely simple, contrived, and naive example:

Code: [Select]
   sub  esp, 8  ;<-- make room on stack for 2 dwords
   mov  dword [esp], 42
   mov  dword [esp+4], ecx
   mov  eax, dword [esp]

I am assuming of course that you are running on a 32-bit protected mode operating system...


Offline tarek89

  • Jr. Member
  • *
  • Posts: 20
Re: Stack definition problem NASM-32bit-Linux
« Reply #4 on: April 25, 2011, 10:31:12 PM »
ah i got it

so i dont have to declare a size or somthing, i mean i dont have to say something like .STACK 64 (MASM) instead i should SUB ESP,64 ??

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Stack definition problem NASM-32bit-Linux
« Reply #5 on: April 25, 2011, 10:46:27 PM »

For most "normal" programs the default stack size used by the linker is sufficient.
If you need more control check out the --stack option

Offline tarek89

  • Jr. Member
  • *
  • Posts: 20
Re: Stack definition problem NASM-32bit-Linux
« Reply #6 on: April 25, 2011, 10:47:40 PM »
Thats great
Thanks for your great patience and help  ;)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Stack definition problem NASM-32bit-Linux
« Reply #7 on: April 25, 2011, 11:20:50 PM »
It is not immediately obvious (to beginners) that "call" and "ret" use the stack. "call" stores the return address on the stack (roughly like "push return_address"/ "jmp someplace"). When you get to "ret" it gets the return address from the stack (roughly like "pop return_address" / "jmp there"). So when you get to "ret", the return address had "better" be there! If you "pushf" and don't "popf", your program tries to return to whatever address is represented by the flags... which will almost certainly segfault!

As an alternative to removing the "PUSHF" entirely, you could also pop the flags before the "ret"...

Code: [Select]
;--------------------------Procedure _print-----------------
_print:
PUSHF
MOV EAX,4 ;sys_write
MOV EBX,1 ;file descriptor 1
INT 80h                    ; Call Kernel
                POPF ; <---
RET                          ; Return

I haven't tested that, but I'm pretty sure it'll work.

What Rob has shown you is how to reserve space for local variables on the stack - useful, but I don't think it's what you're asking. We can "use" the stack without "declaring" the stack (segment stack stack) because the OS does it for us. Linux works with "pages" of memory - blocks of 4096 bytes. When our program loads, Linux give us a "page" of stack (maybe more than one?). Immediately below that, Linux puts a "guard page" - a page marked "not present". If/when we try to write to that "guard page", it causes an exception. Linux sees that it's from the stack, and allocates a new page for us (and puts a new "guard page" under that). Fortunately, you don't need to understand the details of how that works to use it. You've got plenty of stack - just go ahead and use it. If you want more details, look up "virtual memory" (it's fairly complicated - a "CPU thing", not a "Nasm thing", so you won't find it in the Nasm Manual).

Best,
Frank


Offline tarek89

  • Jr. Member
  • *
  • Posts: 20
Re: Stack definition problem NASM-32bit-Linux
« Reply #8 on: April 26, 2011, 04:23:52 PM »
Wow thank  you so so much for your clarification  :D

it really helped me understanding things, the code above was my fault i was just testing wether i can use stack or not i didnt think about the code wise

thank you all for your care