LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-20-2020, 08:54 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 308
Blog Entries: 2

Rep: Reputation: 16
Question Mystery address present: Callee stack asm instruction block.


Take the following extremely simple program:
Code:
	#include <stdio.h>
	
	//PROTOTYPES
		int sumOfTwo(int, int);
		
	//Main
		int main()
		{
			int x, y, z;
			x = 5;
			y = 13;
			z = sumOfTwo(x, y);
			printf("z = %d", z);
			return 0;
		}
	
	//FUNCTIONS
		int sumOfTwo(int a, int b)
		{
			int sum = a + b;
			return sum;
		}
when running
Code:
gcc -S add.c
produces the following ASM code:
Code:
	.file	"add.c"
	.section	.rodata
.LC0:
	.string	"z = %d"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	$5, -12(%rbp)
	movl	$13, -8(%rbp)
	movl	-8(%rbp), %edx
	movl	-12(%rbp), %eax
	movl	%edx, %esi
	movl	%eax, %edi
	call	sumOfTwo
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %esi
	movl	$.LC0, %edi
	movl	$0, %eax
	call	printf
	movl	$0, %eax
	leave
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.globl	sumOfTwo
	.type	sumOfTwo, @function
sumOfTwo:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	%edi, -20(%rbp)
	movl	%esi, -24(%rbp)
	movl	-20(%rbp), %edx
	movl	-24(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE1:
	.size	sumOfTwo, .-sumOfTwo
	.ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
	.section	.note.GNU-stack,"",@progbits
or in simplfied ASM code:
Code:
main:
	pushq	%rbp
	movq	%rsp, %rbp
	subq	$16, %rsp
	movl	$5, -12(%rbp)
	movl	$13, -8(%rbp)
	movl	-8(%rbp), %edx
	movl	-12(%rbp), %eax
	movl	%edx, %esi
	movl	%eax, %edi
	call	sumOfTwo
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %esi
	movl	$.LC0, %edi
	movl	$0, %eax
	call	printf
	movl	$0, %eax
	leave
	ret
sumOfTwo:
	pushq	%rbp
	movq	%rsp, %rbp
	movl	%edi, -20(%rbp)
	movl	%esi, -24(%rbp)
	movl	-20(%rbp), %edx
	movl	-24(%rbp), %eax
	addl	%edx, %eax
	movl	%eax, -4(%rbp)
	movl	-4(%rbp), %eax
	popq	%rbp
	ret
I would like to focus on the following asm instruction block, it is seen quite often in asm code:
Code:
	pushq	%rbp
	movq	%rsp, %rbp
I would like to understand the specifics of the above. What I understand so far is:
Code:
pushq %rbp
means first:
Code:
subq $8, %rsp
The above is equivilent to rsp = rsp - 8; Anotherwords increment stack pointer by 8(q) bytes (stack memory increments/grows downwards, therefore "-8"). This is quite significant because this new 8 byte block will be the first block of the new stack(callee stack).

And second:
Code:
movq %rbp, (%rsp)
This means move the 8 bytes in "stack(caller stack) base pointer" register into the block of memory(5 different types of memory, this specific memory is stack memory) in stack pointed to by the memory address currently in register %rsp.
*** (memory address in %rsp??) ***

Notice how above instruction contains (%rsp) as its destination. To my knowledge when instructions other than "lea" use parentesis, this denotes Indirect Mode Addressing, which means the register surrounded by "()"'s content is an address in memory(stack memory). Please correct me if this understanding is incorrect.

Finally I will interpret the second line of instuction above:
Code:
movq %rsp, %rbp
means to move the contents of stack pointer register(%rsp) into %rbp register. Anotherwords, this will move the memory address of the previous stack's base pointer into the base pointer register.

Please feel free to correct my above understanding/interpretation.


QUESTION:
But wait! Look again above where I previously said: *** (memory address in %rsp??) ***.
How/Why in the first place is there a stack memory address currently in stack pointer register (%rsp)? How do you know there isn't an actual value in %rsp prior to the pushq %rbp instruction? Can %rsp, %rbp registers only have memory addresses in them?


If this isn't an appropriate forum to post this question, any suggestions to where should I post this question?
 
Old 06-21-2020, 03:50 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,056

Rep: Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469
> How/Why in the first place is there a stack memory address currently in stack pointer register (%rsp)?

That is preset by kernel/libc.

> How do you know there isn't an actual value in %rsp prior to the pushq %rbp instruction?

You have to suppose that %rsp contains the address of the current position on the stack. Its name implies
this, too.

> Can %rsp, %rbp registers only have memory addresses in them?

No: in your own Assembly program, you don't have to follow the standards/conventions; but if you don't, your program won't be compatible with other programs (like libc).

Last edited by NevemTeve; 06-21-2020 at 03:51 AM.
 
2 members found this post helpful.
  


Reply

Tags
asm, register, stack


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Virtualbox 5.0.14 from SBo error Callee RC: NS_ERROR_FACTORY_NOT_REGISTERED (0x80040154) khronosschoty Slackware 2 10-06-2016 02:35 PM
[SOLVED] VirtualBox: NS_ERROR_FAILURE, component Machine, interface IMachine, callee catkin Linux - Virtualization and Cloud 1 12-11-2010 07:56 AM
single 8K process stack vs 4K process stack and a seperate 4K interrupt stack charvak Linux - Kernel 1 03-17-2010 06:58 PM
ASM x32 vs ASM x64 Tegramon Programming 3 02-27-2008 02:26 PM
Which linux instruction is equivalent to windows' instruction "tracert"? backpacker Linux - Software 1 04-04-2006 10:55 PM

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

All times are GMT -5. The time now is 05:11 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration