Reputation: 19
after running this program i got a bug 'Segmentation fault (core dumped)' im using Ubuntu 14.04 can someone tell me what kind of error this is and how to solve it? here is the code:
#include<stdlib.h>
#include<stdio.h>
typedef struct {
char hex[17];
char dezimal[21];
}Stu;
extern Stu* _structfunc(int n);
int main(int argc, char *argv[])
{
Stu* a = _structfunc(123456);
printf("%s,%s",a->hex,a->dezimal);
free (a);
return 0;
}
LEN_HEX equ 16
LEN_DEZ equ 10
LEN_STRUCT equ LEN_HEX + 1 + LEN_DEZ + 1
SECTION .data
base10 dq 10
base16 dq 16
SECTION .text
global _structfunc
extern malloc
extern free
_structfunc:
push rbp
mov rbp,rsp
push rbx ;save the previous value in rdi
push rdi ;s.o.
mov rdi,LEN_STRUCT
call malloc ;adress in rax
mov rbx,rax
mov rax,[rsp]
mov rcx,LEN_HEX
hex:
mov rdx,0
div qword [base16]
cmp rdx,10
jae hex_charactor
add rdx,0x30
jmp hex_end
hex_charactor:
add rdx,0x37
hex_end:
mov [rbx+rcx-1],dl
loop hex
mov byte [rbx+LEN_HEX],0
mov rax,[rsp]
mov rcx, LEN_DEZ
dez:
mov rdx,0
div qword [base10]
add rdx,0x30
dez_end:
mov [rbx+LEN_HEX+1+rcx-1],dl
loop dez
mov byte [rbx+LEN_STRUCT-1],0
add rsp,8
pop rbx
pop rbp
ret
the function should give a struct address back ,which include the hexadecimal form of 123456 and decimal form of 123456
Upvotes: 1
Views: 683
Reputation: 12435
Segmentation fault means it tried to access an invalid address.
The cause is that it doesn’t load rax with the return value (from rbx) before the ret.
Generally you would use a debugger to determine what line of code failed and what the invalid address is in order to track down where the invalid address came from.
Upvotes: 1