Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>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.



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.


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: