Assembly question

Programming, for all ages and all languages.
Post Reply
ChrisSkura
Member
Member
Posts: 33
Joined: Sat Nov 07, 2009 2:47 am

Assembly question

Post by ChrisSkura »

I've been writing my OS and I've been using the

Code: Select all

cmp 0,1
je equal
jne not_equal
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.
smeezekitty
Member
Member
Posts: 50
Joined: Sat Mar 21, 2009 9:42 pm

Re: Assembly question

Post by smeezekitty »

some assemblers support .IF buts its basically a hidden jump
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Assembly question

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

Code: Select all

je equal
jne not_equal
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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Assembly question

Post 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.
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: Assembly question

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