(I am not a native speaker of the English language, sorry for the grammatical errors)
I couldn't find any good tutorial about mixing c and assembly(please recommend me if you have one) and started to figure it out by myself. However I got stuck when I tried to combine file input/output in assembly with a c main file. The print function prints out some random garbage I think the problem can be found with the parameters. If needed inform me about best practices. Thank you.
/* main.c */
extern int createfile(char filename[], int premissions);
extern int openfile(char filename[], int mode, int premissions);
extern void writefile(int len, char * str, int fd);
extern void readfile(int fd, char * str,int bs);
extern void closefile(char fd);
extern void print(int len, char * str);
int main() {
int fd = createfile("Hello world", 0777);
char str[4] = "HOPE";
writefile(4, str, fd);
closefile(fd);
fd = openfile("Hello world", 0, 0777);
char bf[4];
readfile(fd, bf, 4);
print(4, bf);
closefile(fd);
}
; file.asm
; Needed to use the functions in c
SECTION .TEXT
GLOBAL createfile
GLOBAL openfile
GLOBAL writefile
GLOBAL readfile
GLOBAL updatefile
GLOBAL closefile
GLOBAL print
createfile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 12 ; Making space for local arguments
mov eax, 8 ; System call number (sys_write)
mov ebx, [ebp + 8] ; Filename
mov ecx, [ebp + 12] ; File permissions
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
openfile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 16 ; Making space for local arguments
mov eax, 5 ; System call number (sys_write)
mov ebx, [ebp + 8] ; Filename
mov ecx, [ebp + 12] ; File acces mode
mov edx, [ebp + 16] ; File premissions
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
writefile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 16 ; Making space for local arguments
mov edx, [ebp + 8] ; Number of bytes
mov ecx, [ebp + 12] ; Message to write
mov ebx, [ebp + 16] ; File descriptor
mov eax, 4 ; System call number (sys_write)
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
readfile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 16 ; Making space for local arguments
mov eax, 3 ; System call number (sys_write)
mov ebx, [ebp + 8] ; File descriptor
mov ecx, [ebp + 12] ; Pointer input buffer
mov edx, [ebp + 16] ; Buffersize
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
updatefile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 16 ; Making space for local arguments
mov eax, 19 ; System call number (sys_write)
mov ebx, [ebp + 8] ; File descriptor
mov ecx, [ebp + 12] ; Offset value
mov edx, [ebp + 16] ; Reference to offset
int 0x80 ; Call kernel
mov esp, ebp
closefile:
push ebp ; Include arguments
mov ebp, esp
sub esp, 8 ; Making space for local arguments
mov eax, 6 ; System call number (sys_write)
mov ebx, [ebp + 8] ; File descriptor
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
print:
push ebp ; Include arguments
mov ebp, esp
sub esp, 16 ; Making space for local arguments
mov edx, [ebp + 8] ; Number of bytes
mov ecx, [ebp + 12] ; Message to write
mov ebx, 1 ; File descriptor
mov eax, 4 ; System call number (sys_write)
int 0x80 ; Call kernel
mov esp, ebp ; Clean up
pop ebp
ret ; Return control
I use a intel processor and Arch linux is my os.
Too compile and run I use:
nasm -f elf32 file.asm
gcc -m32 -o bin.o main.c file.o
./bin