Sign in to follow this  
johnnyBravo

Whats wrong with my 'if statement' in assembly?

Recommended Posts

Hi, i'm doing some assembly and I'm trying to do an if statement, if cc == 0 set reg5 to 1 else set reg5 to 0
comp r3, r6     // 
beq L1          //if cc == 0 goto L1
set 00, r5      //else if cc != 0, set r5 to 0
//problem:keeps going to L1 from here
L1: set 01, r5  //set r5 to 1

The problem is 'if cc !=0' it sets 'r5' to 0 fine, but continues to 'L1', and sets it to 1 aswell. What am I doing wrong? thx!

Share this post


Link to post
Share on other sites
Maybe you should handle a '!=' (or "if (r3 != r6)") and send it to L2, if it reaches that point. I'm no expert in Assembly, but that's my guess. =)

Share this post


Link to post
Share on other sites
you need an unconditional jump after you set r5 to 0.

I forget the syntax but something like


comp r3, r6
beq L1
set 00, r5
jmp L2
L1: set 01, r5
L2: whatever you want to do

what you have now is equivalent to

if (r3 == r6)
r5 = 0;
r5 = 1;

Share this post


Link to post
Share on other sites
Ah right I see,

With the
L2:

I want it to goto that position, but I don't want to put any code next to it, but I can't compile it unless I do.

What should I do?

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
hmmmm, nothing, i just want the L2: so I can skip calling L1:


Yes, but what happens _after_ that? Something must.

Quote:
I can't compile it unless I do


What error message do you get?

Quote:
Dude: CMOV


Except this does not look like x86 assembly.

Which architecture is it, BTW?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes, or what sort of assembly dialect is it?

Share this post


Link to post
Share on other sites
I'm not really sure which dialect it is, I'm using 'Yet Another Assembly Simulator' written by someone at Macquarie Uni, our lecturer said its x86.

the error is some numbers 'opcode not recognized', over and over.

I ended up just storing it:
L2: store r5, 01

Which wasn't part of the question guidelines.


Share this post


Link to post
Share on other sites
Edit: woop. My answer has nothing to do with your question. Funnay...

It looks like some MIPS dialect. In this case, remember that you have a 4 stage pipeline, and that any jump code will flush the two last stages. meaning that

set 00, r1
ja L1
set 00, r2 ; (1) this line is still in the pipeline and WILL BE EXECUTED
L1:

(1) is said to be in the "last delay slot". To avoid the execution of (1), put a nop right after the jump.

In your case, the beq (branch is equal) does the same. "set 00, r5" is in the LDS, and is executed.


Regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this