LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2020, 09:09 AM   #1
Sunless
LQ Newbie
 
Registered: Dec 2019
Posts: 17

Rep: Reputation: Disabled
Translate NASM program into GNU Assembler one


Hi all!
There are some code in C and Assembly here https://montcs.bloomu.edu/Informatio...asm-stack.html See examples add2.asm in NASM.
I didn't understand square brackets operation. How to translate
Quote:
mov eax, [ ebp + 8 ]
into GNU Assembler call? What does 8 mean in square brackets? I assume that sizeof(int) has 4 value. Thank you.
 
Old 09-20-2020, 09:30 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Code:
mov eax, [ebp + 8]
Square brackets dereference and intel syntax the parameters are reversed, so move the value at ebp+8 to eax.
 
2 members found this post helpful.
Old 09-21-2020, 07:13 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
Intel syntax addressing: [BASE + INDEX * SCALE + DISPLACEMENT]
AT&T syntax addressing: DISPLACEMENT(BASE, INDEX, SCALE)

So, essentially, in AT&T (gas) syntax it would be:
mov 8(%ebp), %eax


Yes, sizeof(int) will be 4. The reason it's ebp + 8 and not ebp + 4 is because of the function preamble ( push ebp ; mov ebp, esp ) which has already pushed ebp to the stack and reloaded ebp from the new stack pointer, which is now pointing 4 bytes lower.

The stack will look like this:
+12 second int arg
+8 first int arg
+4 return address
SP original ebp value (which the function preamble just pushed)

Last edited by GazL; 09-21-2020 at 09:21 AM. Reason: fixed stack example - missing return address.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Crow Translate: Desktop / CLI Text Translation App Using Google Translate, Yandex Translate and Bing Translator LXer Syndicated Linux News 0 05-11-2019 05:13 AM
nasm assembler; output numbers? infinity42 Programming 4 05-26-2018 01:22 PM
Why is esi used in 1 nasm script but edi used in another nasm script? abefroman Programming 6 08-26-2015 02:04 AM
[SOLVED] how to traverse byte array in assembly using nasm as assembler? tushs Programming 3 06-25-2010 01:18 AM
NASM assembler code, why is it outputting a letter? Omelas0469 Programming 2 03-15-2007 11:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration