Page 1 of 1
Assembly question
Posted: Fri Nov 13, 2009 3:21 pm
by ChrisSkura
I've been writing my OS and I've been using the
method and I have been doing this for ALL of my comparing things.
I just want to know if there is an if statement like there is in C++
that doesn't jump to different locations like CMP does.
Re: Assembly question
Posted: Fri Nov 13, 2009 4:58 pm
by smeezekitty
some assemblers support .IF buts its basically a hidden jump
Re: Assembly question
Posted: Fri Nov 13, 2009 5:33 pm
by Combuster
A processor has no notion of what an if or a for block is - it works on the level of "goto", "if x goto", "remember this place and goto", ... Assembly is *not* C - Trying to program a language with the concepts of another is guaranteed to have sub-par results.
Don't think about converting to assembly, think
in assembly.
Some related code snippets: (if you don't understand them, please read the manuals first.)
The last opcode is redundant - if it is not equal, then the not equal jump will always be taken. You normally put code here.
Code: Select all
je equal
not_equal:
bla eax
bla ebx
jmp rest
equal:
bla ecx
bla edx
rest:
; continue here
And then there are conditional moves (movcc, setcc). Not that you can emulate them with a jump, or even without:
Code: Select all
;equivalent to setnae/setb/setc al, or salc
sbb al, al
Code: Select all
; value = (i < j) ? a : b
; while avoiding unnecessary pipeline flushes
mov eax, a
mov edx, b
mov ecx, i
; this is cmp ecx, j; cmovc eax, edx; in 5 386-friendly instructions. no jumps needed.
sub edx, eax
cmp ecx, j
sbb ecx, ecx
and ecx, edx
add eax, ecx
Re: Assembly question
Posted: Fri Nov 13, 2009 8:05 pm
by bewing
I think it would be very educational for you to take a simple C program with some "if"s in it, compile it with GCC, and then disassemble it with GDB or whatever.
All the high level code that you know is built out of these jumps -- effectively GOTOs. And the compiler has to do a hell of a lot of work to convert all your "if"s into GOTOs.
In ASM, there are times when you don't need to bother doing the cmp statements -- especially if you are testing the result of a calculation against 0. But you still need the conditional jumps.
As combuster said, you only need half of the ones that you seem to be putting in.
"test" is also a better replacement for "cmp" in some situations.
Re: Assembly question
Posted: Sat Nov 14, 2009 3:02 am
by fronty
bewing wrote:I think it would be very educational for you to take a simple C program with some "if"s in it, compile it with GCC, and then disassemble it with GDB or whatever.
Or just use GCC to produce assembly.