5 ms·
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:
by Annatar 10y ago
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 address registers):
cmp.l #5, d0 ; d0 is unchanged by the comparison
bgt MoreThanFive
;
; substract the value of d1 from d0, and store the result
; in d0.
;
sub.l d1, d0
however, we don't usually branch if greater or lower; we simply compare whether a register is equal to some value:
cmp.l #5, d0
bne NotFive
- deleted 10y ago[deleted]
- vram22 10y ago>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? I haven't done assembly code for a while, and was not an expert at it earlier, so guessing, but: it may be because of what flags in the flags register (if there is one nowadays) get set - they could be different for the two versions of your comparison.
- Annatar 10y agoYes, 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.
- vram22 10y ago>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, the opposite (maybe not for all the bits) of what would get set on comparing B to A; because I thought it would be done by subtracting A from B or B from A, and then setting (some of) those flag bits based on which was greater or equal. If that is so, comparing A to B will not have the same result in the register as comparing B to A. And the reason why I think so, is that there are assembly instuctions like JGE (Jump if Greater or Equal), JE (Jump if Equal), JNE (Jump if Not Equal), etc. - the meaning of those instructions would get changed and so would the resulting action (jump or not jump) based on the looking at the flags set on comparing A to B vs. B to A.
- Annatar 10y agoYou'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.
- vram22 10y agoI think we might be talking about different things, or past each other. Let it go.
- tripzilch 10y agoin 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.