LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-15-2006, 02:35 PM   #1
tokernizer
LQ Newbie
 
Registered: Nov 2005
Distribution: Ubuntu 5.10
Posts: 6

Rep: Reputation: 0
assembly and c


I am trying to modify the value of a member in a c structure by calling a function written in assembly. Here are the codes:
c code:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct integer *intpointer;
typedef struct integer{
	int a;
}integer;

extern int zero(intpointer);
int main(){
	intpointer a = (intpointer)malloc(sizeof(integer));
	int b;
	a->a = 33;
	printf("before: %d\n", a->a);
	b = zero(a);
	printf("after: %d\n", a->a);
	printf("should be: %d\n", b);
	return 0;
}
assembly code:
Code:
global zero
zero:
	push ebp
	mov ebp, esp
	mov dword [ebp + 12], 0
	mov eax, [ebp + 12]
	pop ebp
	ret
I was expecting to see the output as:
Code:
before: 33
after: 0
should be: 0
but it came out as:
Code:
before: 33
after: 33
should be: 0
what am i doing wrong ?
 
Old 04-15-2006, 03:44 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
A few things.

If I'm not mistaken, the first pass arg is at esp+4, since your pushing ebp, it will be ebp+8 (or, esp+8). So you want the value at ebp+8, not ebp+12.

Second, the value at ebp+8 (12 you have) will be an address. The indirect addressing mode your using ([ebp+8]) will only get you the address, since what your getting is the value at the address that ebp holds, plus 8, not the address, of the address, there. All your doing is overwriting some value on that stack that the other program will never see. You should have something like this,
Code:
push ebp
push ebx
mov ebp, esp

;; It's +12 in this case because I pushed
;; ebx AS WELL as ebp =]
mov ebx, dword [ebp+12]
mov dword [ebx], 0
mov eax, dword [ebx]

mov esp, ebp
pop ebx
pop ebp
ret
You'll also notice, I restore my stack afterwards, something you don't. You should move the value of ebp back into esp. This just ensures that everything will be left untouched, after the function runs.
 
Old 04-15-2006, 03:57 PM   #3
tokernizer
LQ Newbie
 
Registered: Nov 2005
Distribution: Ubuntu 5.10
Posts: 6

Original Poster
Rep: Reputation: 0
I got it now, thank you.
 
Old 04-15-2006, 04:01 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
It's nice to see your using NASM. I am not a big fan of the AT&T style of gas
 
Old 04-15-2006, 09:19 PM   #5
llmmix
Member
 
Registered: Jun 2005
Posts: 73

Rep: Reputation: 15
tokernizer:

How to compile your example? I like to know.

gcc -c a.c b.s -o c ?

i love gas anyway it standard..
 
Old 04-15-2006, 09:27 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Well, he's using nasm, so it would be like,

Code:
nasm -f elf asm_code.asm
gcc -c c_code.c
gcc asm_code.o c_code.o -o executable

Last edited by 95se; 04-15-2006 at 09:28 PM.
 
Old 04-16-2006, 02:51 AM   #7
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
Quote:
Originally Posted by 95se
It's nice to see your using NASM. I am not a big fan of the AT&T style of gas
Ditto .. nasm is nice
 
Old 04-16-2006, 08:19 AM   #8
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Starting with binutils 2.10, gas understands the Intel syntax; it is triggered with the .intel_syntax directive.
 
Old 04-16-2006, 11:40 AM   #9
llmmix
Member
 
Registered: Jun 2005
Posts: 73

Rep: Reputation: 15
Quote:
Originally Posted by 95se
Well, he's using nasm, so it would be like,

Code:
nasm -f elf asm_code.asm
gcc -c c_code.c
gcc asm_code.o c_code.o -o executable
Thanks!
By the way, next time i wouldn't post "Thanks" reply,
not because i am tired, but it just too formal and space wasted.

without post, i always Thanks to everybody here in my hearts.
 
  


Reply

Tags
nasm



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
assembly ? blackzone Programming 3 10-15-2004 02:36 AM
Assembly jinksys Programming 3 09-14-2003 04:33 PM
I need help for Assembly skb Programming 10 08-01-2003 04:51 PM
assembly genghis Programming 2 06-12-2003 07:46 AM
assembly sanjay pradhan Programming 1 02-01-2002 10:05 AM

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

All times are GMT -5. The time now is 08:18 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
Open Source Consulting | Domain Registration