Reputation: 185
I am building a compiler for a C-like language, and i'm trying to link a basic "void readString(int, char*)" function implemented in assembly, with assembly generated by my compiler.
The compiled c-like file is
void main () {
char t[20];
readString(7,t); // Read 7 bytes and place them in t buffer
}
After the compilation, The resulting file is: out.s file (notice that the calling convention is: passing arguments through stack. also Integers have 2 bytes size in this language):
.$0:
.globl main
main:
pushq %rbp
movq %rsp,%rbp
subq $20,%rsp
.$1:
movw $7,%ax # Push first argument in the stack
pushw %ax
.$2:
leaq -20(%rbp),%rax # Push address of the second arg in the stack
pushq %rax
.$3:
subq $8,%rsp # this is not important, needed for the convention being followed
pushq 16(%rbp) # pushing "access link",
call _readString
addq $26,%rsp # caller clears the "leftovers"
.$4:
.$main_0_11:
movq %rbp,%rsp
popq %rbp
ret
Code in reads.asm for the library function:
.intel_syntax noprefix
.global _readString
_readString push rbp
mov rbp, rsp
push rdi
push rsi
mov rdi, [rbp+32] # First argument
mov rsi, [rbp+34] # Second Argument
mov rdx, rdi
doRead:
mov byte ptr [rsi], 0x00
xor rax, rax
mov rdi, rax
syscall # read syscall, reads up to $rdx bytes
or rax, rax # nothing read
jz finish #
add rsi, rax #
cmp byte ptr [rsi-1], 0x0a # check if last character read was '\n'
jne addZero #
sub rsi, 1 # if so, replace with '\0'
addZero:
mov byte ptr [rsi], 0x00
finish:
pop rsi
pop rdi
pop rbp
ret
The linking/running follows
$ gcc -c out.s
$ gcc -c reads.s
$ gcc out.o reads.o
$ ./a.out
[2] Segmentation fault ./a.out
A Picture that depicts the calling convention
Upvotes: 0
Views: 537
Reputation: 185
Ok, as @David Hoelzer suggested i checked again the way "readString" grabs the arguments from the stack and they were reversed in order!
So the first lines of reads.asm will become
_readString:
push rbp
mov rbp, rsp
push rdi
push rsi
movzx edi, word ptr [rbp+40] # Get first argument, check comments for more info
mov rsi, [rbp+32] # Second Argument
mov rdx, rdi
Upvotes: 1