Hi,
can someone show me a little program using mixed language, C and Assembly AT&T syntax.
I can't understand how it works :( .
Thanks
Printable View
Hi,
can someone show me a little program using mixed language, C and Assembly AT&T syntax.
I can't understand how it works :( .
Thanks
I suppose the first question is going to be "what's wrong with the 38,756 examples you can find on the web?"
But generally this is probably going to be rather system-specific, but if you're using gcc why not look at the HOWTO or the gcc manual?
Hey,
I will post some code I have.
Code:
// mlpc1.c
// C language source file for mixed language programming example
#include <stdio.h>
void UpperCase(char *Str);
int main() {
char UserString[20];
fputs("Enter a string: ",stdout);
fgets(UserString,19,stdin);
fputs("\nYou entered: ",stdout);
fputs(UserString,stdout);
fputs("\nAfter call to UpperCase this becomes: ",stdout);
UpperCase(UserString);
fputs(UserString,stdout);
fputs("\n",stdout);
}
I don't get how it works...Code:# mpla1.s
# assembly language source file for mixed language programming example
.data
.text
.global UpperCase
UpperCase:
push %ebp
mov %esp,%ebp
push %esi
push %eax
mov 8(%ebp),%esi # make esi point to the string
UCLoop:
movb (%esi),%al
cmp $0,%al
je UCExit
andb $0xdf,%al
movb %al,(%esi)
inc %esi
jmp UCLoop
UCExit:
pop %eax
pop %esi
pop %ebp
ret
.end
So what is difficult about this? This is exactly the same as you writing a C program in two .c files, except you wrote one of your .c files in assembler.
in the Uppercase function.
i don't get why the register
%esi or %eax was pushed
I have just written a simple function to add 2 numbers. And didn't have to push %eax. I could use them directly since they are "global", right?
So when do we push registers?
I know push means returning push a variable to the stack, but I don't really get a pop.
When you pop something off the stack, the esp is subtracted by 4 bytes.
but that content you popped off, is it saved somewhere? What is the difference between it and ret?
Push stores a register value to the stack so that it can be restored later.
Pop gets the value from the stack and places it back in the register.
Of course you have to be careful with this... if you push A B C you need to pop C B A to restore the registers correctly. Also pushing something you do not later pop will cause a misalignment between stack and data...
so basically if it the data we have in the register is to be used later, we only push.
Ok, I will go try and do some experimenting on this.
Thanks.
yeah that does that have something to do with the cdecl format?
the parameters are popped off in the order in which they are pushed?
One thing that is confusing me, when do we push to the stack?
Before the function is called, or after?
Like in
UpperCase
ebp is pushed to the stack. Does that make it a local?
I'm pretty sure you've been linked to this page before. Have you ever tried actually reading that page? (Especially the part where they tell you why you have to push ebp, and why you should push any registers you intend to use in the function.) Start at "calling a __cdecl function".
read it.
cool thanks. Hopefully, I won't be back ;) (doubt it)
em, so
andb 0xdf, %cl
ands 223 with the lower case char.
How can we know what figure to use? Because if this was in the exam I would have failed it instantly. I just played with anding, and it does indeed convert it to upper.
if it was upper to lower, how would i do it? What would be my mask?
Is there an easy of figuring it out?
Thanks
Code:Uppercase E = 69 = 0b01000101
Lowercase e = 101 = 0b01100101
so i have to or it by 0x20
E | 0x20..
that wouldn't work for everything :(