Live data from Hacker News

Learning to Read X86 Assembly Language

patshaughnessy.net

231–238 of 238 posts

Re: Learning to Read X86 Assembly Language

#231

Earlier quoted context omitted.

-O2 will remove stack pointer setup

You have to have stack and frame setup if the code is to be ABI / target platform's calling convention compliant. Which compiler are you referring to, because GCC won't remove it, as far as I could tell looking at the generated assembler code? You could purposely tell GCC to omit the frame pointer -fomit-frame-pointer, but then you just made the code extremely difficult to debug (and stack setup code will still be ge…

you dont need to setup local stack if your code doesnt use stack

Re: Learning to Read X86 Assembly Language

#232
post #228

Earlier quoted context omitted.

Yes, there is a status register, every processor must have one (or else the processor couldn't function). Doesn't matter whether you compare 5 to a register (or memory location, depending on the processor), or memory / register to 5, the same bit(s) will still be set in the status register.

>Doesn't matter whether you compare 5 to a register (or memory location, depending on the processor), or memory / register to 5, the same bit(s) will still be set in the status register. Are you sure? That was my whole point - that it may not be that way. As I said, it's been a while, but it seems to me that the bits that get set in the flags/status register, on comparing A to B, should be, in some sense at least, th…

You're overthinking this way more than you need to.

On any given processor, in this context, there is one and only one way to compare an immediate value with one in a register, so you don't have to worry about whether you're comparing 5 to %eax, or eax to 5: you can't subtract the value in %eax from 5, because 5 is an immediate value, not a memory location.

  	.section	__TEXT,__text,regular,pure_instructions
  	.globl	_main
  	.align	4, 0x90
  _main:
  	movl	$17, %eax
  	cmpl	$5, %eax
  	jle	Exit
  	movl	$5, %eax
  Exit:	ret
now let's assemble and link that using the C compiler's front end, so we won't have to worry about _init and _fini:

  > cc cmp.s -o cmp
  > ./cmp; echo $?
  5
note the cmpl $5, %eax instruction. Now watch what happens when I attempt to compare %eax with 5:

  	.section	__TEXT,__text,regular,pure_instructions
  	.globl	_main
  	.align	4, 0x90
  _main:
  	movl	$17, %eax
  	cmpl	%eax, $5
  	jle	Exit
  	movl	$5, %eax
  Exit:	ret

  > cc cmp.s -o cmp
   cmp.s:6:13: error: invalid operand for instruction
   cmpl %eax, $5
              ^~
it can't be done, because there is one and only one way to compare an immediate value with one in a register. intel or AT&T syntax -- dst, src or src, dst -- the comparison is the same. Therefore, AT&T syntax is the best thing since sliced bread, because it's left to right instead of right to left, which is how we think in terms of taking something and moving it somewhere -- in the physical world, step 1. will be to take an object and step 2. will be to move that object somewhere.

Re: Learning to Read X86 Assembly Language

#233

Earlier quoted context omitted.

You have to have stack and frame setup if the code is to be ABI / target platform's calling convention compliant. Which compiler are you referring to, because GCC won't remove it, as far as I could tell looking at the generated assembler code? You could purposely tell GCC to omit the frame pointer -fomit-frame-pointer, but then you just made the code extremely difficult to debug (and stack setup code will still be ge…

you dont need to setup local stack if your code doesnt use stack

Please provide instructions on how to reproduce / verify the assertion that GCC doesn't generate stack / frame pointer setup. I want to see this for myself.

Re: Learning to Read X86 Assembly Language

#234

Earlier quoted context omitted.

I haven't done any 68K Asm and barely glanced at SPARC, but how does src, dst interact with noncommutative operations like subtraction and comparison? E.g. with x86 Intel syntax, cmp eax, 5 ; eax - 5 jg morethan5 ; eax > 5 ? then jump. sub eax, ecx ; eax = eax - ecx This is one of the most confusing things about AT&T x86 --- the comparisons and subtractions have their operands reversed, and you have to identify and m…

This is one of the most confusing things about AT&T x86 --- the comparisons and subtractions have their operands reversed, That is confusing as all hell to me: if I compare x to 5, and 5 to x, it's still the same comparison, so what difference does it make? Anyway, on Motorola 68000 it would look like so, assuming data was in data register 0 (there are eight general purpose data registers, and eight general purpose a…

in x86 assembly it matters because what is comparison exactly? cmp is (very cleverly) defined as equivalent to sub (subtraction) without storing the result, only setting the flags.

the jump if greater/lesser/equal/greater-or-equal/etc instructions are all defined in terms of checking the sign, zero, carry and overflow flags. for instance, jz and je are the same instruction. you just use one or the other if it makes more sense in context (I used to write asm by hand for the art of 4096 byte demos)

I used intel notation, because that's what the Asphyxia tutorials and turbo pascal used. So it's what I'm used to. I don't have much of an opinion about the order except that it was an idiotic decision to swap it, either way, the confusion that caused doesn't weigh against if/which one would be more theoretically "right". All the sigils and pre/postfixes seem messy though.

Re: Learning to Read X86 Assembly Language

#235
post #228

Earlier quoted context omitted.

>Doesn't matter whether you compare 5 to a register (or memory location, depending on the processor), or memory / register to 5, the same bit(s) will still be set in the status register. Are you sure? That was my whole point - that it may not be that way. As I said, it's been a while, but it seems to me that the bits that get set in the flags/status register, on comparing A to B, should be, in some sense at least, th…

You're overthinking this way more than you need to. On any given processor, in this context, there is one and only one way to compare an immediate value with one in a register, so you don't have to worry about whether you're comparing 5 to %eax, or eax to 5: you can't subtract the value in %eax from 5, because 5 is an immediate value, not a memory location. .section __TEXT,__text,regular,pure_instructions .globl _mai…

I think we might be talking about different things, or past each other. Let it go.

Re: Learning to Read X86 Assembly Language

#236

Earlier quoted context omitted.

you dont need to setup local stack if your code doesnt use stack

Please provide instructions on how to reproduce / verify the assertion that GCC doesn't generate stack / frame pointer setup. I want to see this for myself.

The x86-64 ABI does not require frame pointer setup: https://godbolt.org/g/10URYW

Re: Learning to Read X86 Assembly Language

#237
post #170

Also Matt Godbolt's gcc explorer is the the bee's knees for understanding assembly https://godbolt.org/ I think that playing around with it for 2 hours will teach you more than most classes on the topic. It really drives home why interactivity is such a bit deal in education. You should also try writing a script for counting instructions in binaries. It's pretty illuminating. Here are some sample statistics https://w…

Wow, thanks for posting this — it is a fantastic resource! I love it even more for using Intel x86 assembly syntax, instead of the horrible AT&T syntax.

AT&T is still an option. Which is fantastic because it's a quick way to see how the two differ.

Re: Learning to Read X86 Assembly Language

#238

Earlier quoted context omitted.

Please provide instructions on how to reproduce / verify the assertion that GCC doesn't generate stack / frame pointer setup. I want to see this for myself.

The x86-64 ABI does not require frame pointer setup: https://godbolt.org/g/10URYW

That's a contrived example of inlining; now watch what happens when you provide more than just a trivial function which can be inlined; look at all the futzing it does with the stack:

https://godbolt.org/g/tjrKNO

without explicit direction from humans, compilers can't generate code which calls subroutines or functions without using the stack at the very minimum, and that was my point.

And that's just GCC on intel; check out intel's compiler, it's even worse (click on "turn off intel syntax", and "compile to binary and disassemble the output"):

https://godbolt.org/g/IjGdAP

...and if you try other processors as well, it just gets worse and worse:

https://godbolt.org/g/DZDLju

https://godbolt.org/g/s47tlR

Post reply on HN