Reputation: 392
I am building a subset-of-C Compiler in C# on Linux using Monodevelop. My compiler reads text from a file and outputs to assembly. Now I would first have to note that the problem I get is not a C# problem, but an assembly misunderstanding. I am using nasm assembler and gcc linker. Here is the outputed assembly:
extern printf
extern strcpy
extern systemsame
extern readinput
extern getdest
section .text
global main
jmp main_over
main:
push rax
mov rax, 0
mov rax, 1
mov byte [a], al
pop rax
push rax
mov rax, 0
mov rax, 0
mov byte [b], al
pop rax
IF_STATEMENT_0:
cmp byte [a] , 1
jne IF_STATEMENT_0_over
mov rdi, QUOTE_0
mov rax, 0
call printf
ret
main_over:
IF_STATEMENT_1:
cmp byte [a] , 0
jne IF_STATEMENT_1_over
mov rdi, QUOTE_1
mov rax, 0
call printf
ret
IF_STATEMENT_1_over:
IF_STATEMENT_2:
cmp byte [a] , b
jne IF_STATEMENT_2_over
mov rdi, QUOTE_2
mov rax, 0
call printf
ret
IF_STATEMENT_2_over:
IF_STATEMENT_3:
cmp byte [a], 0
je IF_STATEMENT_3
mov rdi, QUOTE_3
mov rax, 0
call printf
ret
IF_STATEMENT_3_over:
IF_STATEMENT_4:
cmp byte [b], 0
je IF_STATEMENT_4
mov rdi, QUOTE_4
mov rax, 0
call printf
ret
IF_STATEMENT_4_over:
ret
IF_STATEMENT_0_over:
section .bss
a: resb 1
b: resb 1
tempbyte: resb 20
tempword: resw 20
tempdword: resd 20
section .data
;Passon extra bit from parsing state;;;
QUOTE_0: db "a is 1!", 0
QUOTE_1: db "a is 0!", 0
QUOTE_2: db "a is b!", 0
QUOTE_3: db "a is true!", 0
QUOTE_4: db "b is true!", 0
I compile this with nasm assembler and get NO Errors and NO Warnings.
I then get an object code (.o
) file and link it with another Working (tested) object code file (which has the definitions for systemsame()
, readinput()
, and getdest()
)
using gcc. I get the following strange error:
In function `IF_STATEMENT_1_over':
outout.asm:(.text+0x71): relocation truncated to fit: R_X86_64_8 against `.bss'
collect2: error: ld returned 1 exit status
Upvotes: 3
Views: 104
Reputation: 9377
It seems to me that the error is likely to be cmp byte [a] , b
since that is just after the label mentioned in the error message, and involves symbols defined in .bss
, the section mentioned in the error message. The instruction itself looks suspicious to me, but the exact error isn't quite clear: perhaps the address of b
is being used as an immediate, which would involve its truncation to 8 bits.
Double check that this is in fact the translation that you want. I suspect that you should be loading the value at b
into a spare register and then using that as the second operand to cmp
.
As a side note, you should use "local" labels for basic blocks internal to a function. Otherwise the assembler will waste time and space creating useless relocations. Usually this involves nothing more than prefixing those labels in some way. For example for GNU as
it is .L
- consult the manual of the assembler that you are targeting.
Upvotes: 3