Translation from NASM to GAS

2019-02-20 01:52发布

问题:

how do I translate mov [ebx], al from NASM to GAS? I tried mov %al, (%ebx) but it does segmentatiob fault.

Another question, lets say I have an array in GAS .lcomm array, 50 Do I have to put a dollar($) sign in array like this: mov %rbx, $array or need not to?

Any answer would be helpful :)

回答1:

How about intel2gas?

usage: intel2gas [options] [-o outfile] [infile]
where options include:
    -h        this help
    -i        convert from intel to at&t format (default)
    -g        convert from at&t to intel format
    -m -t     convert from masm/tasm to at&t format
    -c        understand C style comments
    -I        convert inline assembler (intel to at&t only)
    -d        output all % chars as %%
    -V        show version
    infile and outfile default to stdin/stdout


回答2:

Assemble with NASM, disassemble with objdump -d to get the AT&T syntax version of any single instruction.

Or use Agner Fog's objconv to disassemble to a .S file with GAS AT&T syntax you can actually assemble directly. (it puts labels on branch targets). It still doesn't really handle converting data, though, mostly code I think.


BTW, with GAS you can use .intel_syntax noprefix, but it's MASM-style not NASM, so mov eax, symbol is a load, not a mov-immediate of the address. (You'd need mov eax, OFFSET symbol in GAS .intel_syntax). See https://stackoverflow.com/tags/intel-syntax/info for more about the differences between NASM and MASM.


mov [ebx], al and mov %al, (%ebx) are the same instruction: a one-byte store of al using the ebx alone as the address. If that segfaulted, then that instruction alone isn't the problem. Either other instructions are wrong, or you have a different kind of problem. Perhaps you assembled your GAS code as 64-bit? Assembling 32-bit binaries on a 64-bit system (GNU toolchain).



回答3:

you can use objdump (otool on osx) to disassemble into gas format.



回答4:

First up, mov %al, %ecx can't really fault because it's not accessing memory. Hell, it shouldn't even assemble because they are of different size. That said, the correct translation of your nasm code is mov %al, (%ecx) which will write al into memory at address ecx.

For the other question: you need the $ if you want to reference the address. As such, mov %rbx, $array is invalid because you can't assign to the address. If you want to store rbx into the first item of the array, write mov %rbx, array. If you want to load the first item, use mov array, %rbx. If you want to load the address of the first item, do mov $array, %rbx or use lea which is special and doesn't need the $.