Remember Me Forgot your Password?

Forum: Electrical & Computer Engineering :: Computer Organization :: MIPS  New Topic
Problem #24 Reply
Author Message
nak31 Problem #24 Sun 4 Oct 2009 3:29:00 PM
Student
Joined Tues 31 Mar 2009
Posts 511

Problem 24:

Translate the following switch statement in C++ into MIPS. Try to use as few registers as possible. Assume the following mapping:
$s1=x, $s2=y, $s3=z, $s0=a.

switch(a){

case 1: x = y + z;

case 2: x = y - z;

case -1: x = x - z;

default: x = z + z;}


Student Rating3.9/5
nak31 Sun 4 Oct 2009 3:29:00 PM
Student
Joined Tues 31 Mar 2009
Posts 511

Solution:

addi $t0, $0, 1                    # case a="1<"/FONT>

addi $t1, $0, 2                   # case a="2<"/FONT>

addi $t2, $0, -1                   # case a="-1<"/FONT>

bne $s0, $t0, L2                  # handle case 1

add $s1, $s2, $s3

j EXIT

L2: bne $s0, $t1, L3           # handle case 2

sub $s1, $s2, $s3

j EXIT

L3: bne $s0, $t2, D            # handle case -1

sub $s1, $s1, $s3

j EXIT

D: add $s1, $s3, $s3

EXIT:


Student Rating3.9/5
1
© Copyright SolveMyProblem.Net 2017