Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - Greater than, less than equal, greater than equal in MIPS

Given two registers $s0, $s1, how can I convert the following pseudocode into MIPS assembly language using only the slt (set on less than) and beq and bne (branch if equal, branch if not equal) instructions.

   if ($s0 > $s1) { goto label1 }
   if ($s0 >= $s1) { goto label2 }
   if ($s0 <= $s1) { go to label3 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm assuming that the pseudocode executes sequentially, so an earlier condition being true means you go there and never reach the later if statements. This makes the last branch guaranteed taken if it's reached at all, so it doesn't even need to be conditional. (Also assuming that this is a MIPS without a branch-delay slot.)

slt  $t1,$s1,$s0      # checks if $s0 > $s1
bne  $t1,$zero,label1 # if $s0 >  $s1, goes to label1
beq  $s1,$s2,label2   # if $s0 == $s2, goes to label2 
# beq  $t1,$zero,label3 # if $s0 <  $s1, goes to label3
b    label3            # only possibility left

If that's not the case, you'll want to implement $s0 >= $s1 as
!($s0 < $s1) with slt $t1, $s0, $s1 / beqz $t1, target, for example,
as shown in Ahmed's answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...