I am writing a compiler and codegen the following code with an error.
The question is mov [esi+4], DWORD temp_fun_2
gives a relative address like
0xc5
using gdb, while the real address is something like 0x555f55c5
. When I call this address, it gives me a segment error. So how can I solve this problem?
The environment is x86
Thanks in advance.
section .text
extern print
extern error
extern equal
global our_code_starts_here
our_code_starts_here:
mov esi, [esp+4]
add esi, 8
and esi, 0xFFFFFFF8
push ebp
mov ebp, esp
sub esp, 4
jmp near temp_after_1
temp_fun_2:
push ebp
mov ebp, esp
sub esp, 0
mov ecx, [ebp+8]
mov eax, [ebp+12]
and eax, 1
cmp eax, 0
jne near error_non_int
mov eax, 10
and eax, 1
cmp eax, 0
jne near error_non_int
mov eax, [ebp+12]
add eax, 10
jo near overflow_check
mov esp, ebp
pop ebp
ret
temp_after_1:
mov [esi+0], DWORD 1
mov [esi+4], DWORD temp_fun_2
mov eax, esi
add eax, 5
add esi, 8
mov [ebp-4], eax
mov eax, [ebp-4]
and eax, 7
cmp eax, 5
jne near error_non_fun
mov eax, [ebp-4]
sub eax, 5
mov eax, [eax+0]
cmp eax, 1
jne near arity_non_equal
push DWORD 12
mov eax, [ebp-4]
sub eax, 5
push eax
mov eax, [eax+4]
call eax
add esp, 8
mov esp, ebp
pop ebp
ret
overflow_check:
push DWORD 3
call error
error_non_int:
push DWORD 1
call error
error_non_bool:
push DWORD 2
call error
error_non_pair:
push DWORD 4
call error
error_too_small:
push DWORD 5
call error
error_too_large:
push DWORD 6
call error
error_index_non_int:
push DWORD 7
call error
error_prim1_non_int:
push DWORD 8
call error
error_non_fun:
push DWORD 9
call error
arity_non_equal:
push DWORD 10
call error