I am compiling the below small code with GNU AS on Linux. The compile command is:

gcc -nostdlib a.s

And it reports the error "relocation truncatted to fit R_386_16 in .text". And the problem caused by the line "movw ax, $ok_msg". Anybody please tell me how to fix this?

(The code must be in 16bit because I want to run it in Real-mode in x86)


/* a.s */
.global _start

        movw $ok_msg, %ax /* <=== WHY THIS CAUSES A BUG? */

ok_msg: .string "OK\n"
Posted on 2008-01-23 20:39:13 by junkoi
Posted on 2008-01-23 20:54:47 by SpooK

I think my code already followed the guide, in which I inserted ".code16".

I notice a line from the above link though: "... So you can write code that runs on 16-bit processors, but only if that code never references memory."

Is that the reason why my code fails to do "movw ax, $ok_msg"???

That still doesnt make sense to me though.

Do you have any idea on how to fix it?

Posted on 2008-01-23 21:04:06 by junkoi
If you want to write 16-bit code, you're better off using fasm or nasm. GAS is primarily written to be a GCC back-end, and GCC is primarily focused on 32- and 64-bit CPUs.

FASM can directly output 16-bit .com and .exe without any linker, as well as a lot of other formats. NASM is a bit more standard and a bit more portable, but requires you to either write .com or find a 16-bit linker for .exe.
Posted on 2008-01-24 05:07:45 by f0dder