Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » asm: calling C procs

Credits go to kazzmir, Kitty Cat, and Oscar Giner for helping out!
This thread is locked; no one can reply to it. rss feed Print
asm: calling C procs
lucaz
Member #4,194
January 2004

Well, this is a homework question, but I need help!

What I need to do is to read 1024 bytes from "filename.txt" using fopen, fread, and fclose using ASM.

in C:

#include <stdio.h>
int main() {
  char buffer[1024];
  FILE* file = fopen("filename.txt","r");
  fread(file,buffer,1024);
  fclose(file);
}

Sure, I can use -S and take a look to the .s but it's very difficult to read... (for me)

So, these are my ideas (ASM):

1extern fopen
2extern fread
3extern fclose
4 
5// data segment
6buffer db 1024 dup(0)
7 
8// segment
9push eax ;save eax
10xor eax, eax ;eax = 0
11push 'filename.txt' ;arg1
12push 'r' ;arg2
13call fopen
14pop eax ;eax = FILE*
15push eax ;arg1
16push offset buffer ;arg2
17push length buffer ;arg3
18call fread
19push eax ;arg1
20call fclose
21pop eax ;restore eax

I know it sucks, but anyone can help me?

kazzmir
Member #1,786
December 2001
avatar

I havent used x86 asm in a while, but I dont think you can just push strings onto the stack. You have to have a data section or something and give them labels, then move the labels into registers and push the registers onto the stack. For easy reference on how to do this, just use gcc -S :)

lucaz
Member #4,194
January 2004

yes is true.
The -S flag include a lot of stuff and "advanced asm" I don't understand it :(

Oscar Giner
Member #2,207
April 2002
avatar

C functions are prepended with a '_', so the correct function names are _fopen, _fread and _fclose.

[edit]
You can't push strings or characters into the stack. You have to create a string in the data segment with the string contents and push the address.

lucaz
Member #4,194
January 2004

Ok, if I change the string stuff, this is a good sketch?

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

C functions are prepended with a '_'

Not necesarilly. They aren't in Linux, but they are in some versions of MinGW.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Oscar Giner
Member #2,207
April 2002
avatar

The xor is useless, you don't want to set eax to 0 for anything.

Funaction arguments are pushed in reverse order, from right to left.

Remove the pop after the call to fopen.

I don't know what offset and length are. I've never seen them.

But if it's homework... what is your teacher doing? Not teaching you, certainly :P

Quote:

but they are in some versions of MinGW.

They're in all versions of MingW I've tryed. Also in MSVC and in the old TurboC. I though that was part of the standard. I don't know why this is not true in linux.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

They're in all versions of MingW I've tryed.

Same here, but APEG prepends its asm functions with _ in MinGW and the C code refuses to recognize them for some people. Even removing the _ apparently doesn't do anything. :/

As for the xor, I see a lot of assembly code do that to set a register to 0. I just assumed it was more CPU efficient.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Oscar Giner
Member #2,207
April 2002
avatar

Quote:

As for the xor, I see a lot of assembly code do that to set a register to 0. I just assumed it was more CPU efficient.

Yes, xoring is faster than a 'mov eax, 0', but he doesn't need to set eax to 0 in the first place ;)

[edit]
One more thing. This may depend on the assembler you use, but I think the keyword for importing symbols is 'public', not 'extern'.

lucaz
Member #4,194
January 2004

cookies!

Lucas pepe
Member #5,568
March 2005
avatar

okay, then, the code approximated is this: ?

1 
2extern fopen // or public fopen
3extern fread
4extern fclose
5 
6// data segment
7buffer db 1024 dup(0)
8 
9// segment
10push eax ;save eax
11xor eax, eax ;eax = 0
12push 'r' ;arg2
13push 'filename.txt' ;arg1
14call fopen
15// pop eax ;eax = FILE* -- I don't understand because the pop is not necessary
16 
17push length buffer ;arg3
18push offset buffer ;arg2
19push eax ;arg1
20call fread
21push eax ;arg1
22call fclose
23pop eax ;restore eax

// When use the buffer??

Lukas

Oscar Giner
Member #2,207
April 2002
avatar

You've created a 2nd account?

Quote:

I don't understand because the pop is not necessary

The return value is stored in eax, not in the stack. So fopen will put FILE* in eax.

Have you tried that code and see if it works? I think that would be much easier.

ReyBrujo
Moderator
January 2001
avatar

You should also check eax is different from NULL after opening the file.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

lucaz
Member #4,194
January 2004

Quote:

You've created a 2nd account?

No, he's a friend (lukas) Im lucaz.
We'll fix the pops.

I'll try the code now.

EDIT:
welcome back reybrujo!, donde estabas?

ReyBrujo
Moderator
January 2001
avatar

Got a user interface course on the last week of January, and then spent most of February doing interface testings and user testings. I missed the hardcore of C programming, though :)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Not necesarilly. They aren't in Linux, but they are in some versions of MinGW.

Its the object file format. elf doesn't require it, but PE/COFF does.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Billybob
Member #3,136
January 2003

BTW, you can push strings on the stack. You just can't use them like that. You have to pass esp as the argument to the function, since the function wants a pointer.

lucaz
Member #4,194
January 2004

Thanks, I get a 7 :)

Go to: