inline assembly questions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

inline assembly questions

Post by ITchimp »

when I code in inline assembly... I usually use generic assembly instruction
such as mov, push, but in linux source assembly i have seen those being written
as movl, pushl, etc, just curious, what are the circumstance that warranted the extra l, or w, or qw

I realize that this should be automatic... when I compile c code into assembly movl pushl
appears. but when should I code explicitly in those?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: inline assembly questions

Post by iansjack »

Well, as one example, what if you do a move immediate to a memory location? How does the assembler know how many bytes you want to move?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: inline assembly questions

Post by bzt »

ITchimp wrote:as movl, pushl, etc, just curious, what are the circumstance that warranted the extra l, or w, or qw
That's AT&T syntax. Sometimes you can omit the size specifier, but in general you should write that out.
iansjack wrote:How does the assembler know how many bytes you want to move?
From the argument.

For example:

Code: Select all

mov [addr], al     <- byte
mov [addr], ax     <- word
Specifying the argument's size is an AT&T syntax specific thing (GAS and GCC inline), other Assemblers, using the Intel syntax do not need that. They are smart enough to figure out the argument's size (NASM, TASM, FASM, etc.).

For immediates, they usually require a size specifier, just like AT&T, but only for immediates. For example in FASM:

Code: Select all

mov word [addr], 1
Cheers,
bzt
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: inline assembly questions

Post by nexos »

That is why I try to hide my ASM in external files. I hate writing inline ASM. I put most of my ASM in external files compiled with NASM. You will have to use inline ASM in some places, but I try to steer clear of it.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: inline assembly questions

Post by iansjack »

But it's nothing to do with whether the assembler is inline or not. Sometimes there is ambiguity about an instruction unless you use a size qualifier.
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

Re: inline assembly questions

Post by ITchimp »

if you do asm volatile, then all of those assembly code will be respected by the, making them a lot closer to the real nasm..

also, between using att syntax/gcc as assembler, and intel syntax and nasm, which combo is better?
User avatar
iman
Member
Member
Posts: 84
Joined: Wed Feb 06, 2019 10:41 am
Libera.chat IRC: ImAn

Re: inline assembly questions

Post by iman »

ITchimp wrote:between using att syntax/gcc as assembler, and intel syntax and nasm, which combo is better?
I would like to use Intel syntax. Many assemblers including NASM, MASM, FASM, and TASM cover this syntax, and in my opinion easier to read and follow.
There is no difference in machine code generated if using Intel of GAS syntax.
Iman Abdollahzadeh
Github
Codeberg
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: inline assembly questions

Post by Octocontrabass »

ITchimp wrote:if you do asm volatile, then all of those assembly code will be respected by the, making them a lot closer to the real nasm..
If you want it to be like NASM, don't use inline assembly. You can write pure assembly files and assemble them with gas (with or without GCC as a frontend).
ITchimp wrote:also, between using att syntax/gcc as assembler, and intel syntax and nasm, which combo is better?
Don't forget that GCC also supports Intel syntax. I prefer NASM since I like its syntax better than GCC's Intel syntax, but you can't write inline assembly with NASM.
Post Reply