Project 3 Solution

$35.00

Category: Tag:

Description

Description

For project three, your objective is to convert the given C++ code into MIPS assembly. Please do not modify the C++ code itself. You are only allowed to make modi cations to the assembly le. Start writing your code below the main: label and above the exit: label. For this project stay BETWEEN these labels.

When doing a C++ to MIPS conversion, the conversion can be done in the following steps:

  • Assign variables to registers. When inspecting code, any constant values in if-statements or expressions may need to be assigned to temporary registers.

  • Initialize variables to registers. (actually put the values into the registers.)

3 Then move onto the rest of the code.

Before you begin, please make sure you click the link on ilearn to create your GitHub repo.

After created please clone this repo with the git clone repo url command.

Expected Output:

Value of a: 25

Value of b: 31

Value of c: 18

Value of d: 49

Submission

When you have completed the assignment please commit all work done to your private repository. This can be done with the following commands:

git add .

git commit -m “some message”

git push

1

Base MIPS Code

1

. data

“nn”

2

e n d l :

. a s c i i z

#

used

f o r

c o u t << e n d l ;

3

a l b l :

. a s c i i z

“Value

o f

a :

#

l a b e l

f o r

a

4

b l b l :

. a s c i i z

“Value

o f

b :

#

l a b e l

f o r

b

5

c l b l :

. a s c i i z

“Value

o f

c :

#

l a b e l

f o r

c

6

d l b l :

. a s c i i z

“Value

o f

d :

#

l a b e l

f o r

d

7

. t e x t

8

9

# a

> $ s 0

10

# b

> $ s 1

11

# c

> $ s 2

12

# d

> $ s 3

13

main :

14

15

16

e x i t :

17

l a

$a0 , a l b l

# p u t s a l b l i n t o a r g 0 ( a0 r e g i s t e r ) f o r c o u t

18

a d d i

$v0 , $0 ,

4

# p u t s 4

i n

v0

which d e n o t e s

we

a r e

p r i n t i n g

a

s t r i n g

19

s y s c a l l

# make a

s y s c a l l

t o

system

20

21

move

$a0 , $ s 0

# p u t s a

i n t o

a r g 0

( a0 r e g i s t e r )

f o r

c o u t

22

a d d i $v0 , $0 , 1

# p u t s 1 i n v0 t o d e n o t e we a r e p r i n t i n g an i n t

23

s y s c a l l

# make a

s y s c a l l

t o

system

24

25

l a

$a0 , e n d l

# p u t s t h e a d d r e s s o f t h e s t r i n g e n d l i n t o a0

26

a d d i $v0 , $0 , 4

# p u t s 4 i n t o v0 s a y i n g we a r e p r i n t i n g a s t r i n g

27

s y s c a l l

28

29

l a

$a0 , b l b l

# p u t s b l b l i n t o a r g 0 ( a0 r e g i s t e r ) f o r c o u t

30

a d d i

$v0 , $0 ,

4

# p u t s 4

i n

v0

which d e n o t e s

we

a r e

p r i n t i n g

an

s t r i n g

31

s y s c a l l

# make a

s y s c a l l

t o

system

32

33

move

$a0 , $ s 1

# p u t s b

i n t o

a r g 0

( a0 r e g i s t e r )

f o r

c o u t

34

a d d i $v0 , $0 , 1

# p u t s 1 i n v0 t o d e n o t e we a r e p r i n t i n g an i n t

35

s y s c a l l

# make a

s y s c a l l

t o

system

36

37

l a

$a0 , e n d l

# p u t s t h e a d d r e s s o f t h e s t r i n g e n d l i n t o a0

38

a d d i $v0 , $0 , 4

# p u t s 4 i n t o v0 s a y i n g we a r e p r i n t i n g a s t r i n g

39

s y s c a l l

40

41

l a

$a0 , c l b l

# p u t s c l b l i n t o a r g 0 ( a0 r e g i s t e r ) f o r c o u t

42

a d d i

$v0 , $0 ,

4

# p u t s 4

i n

v0

which d e n o t e s

we

a r e

p r i n t i n g

a

s t r i n g

43

s y s c a l l

# make a

s y s c a l l

t o

system

44

45

move

$a0 , $ s 2

# p u t s c

i n t o

a r g 0

( a0 r e g i s t e r )

f o r

c o u t

46

a d d i $v0 , $0 , 1

# p u t s 1 i n v0 t o d e n o t e we a r e p r i n t i n g an i n t

47

s y s c a l l

# make a

s y s c a l l

t o

system

48

49

l a

$a0 , e n d l

# p u t s t h e a d d r e s s o f t h e s t r i n g e n d l i n t o a0

2

50

a d d i

$v0 , $0 , 4

# p u t s 4 i n t o v0 s a y i n g we a r e p r i n t i n g a s t r i n g

51

s y s c a l l

52

53

l a

$a0 , d l b l

# p u t s d l b l i n t o a r g 0 ( a0 r e g i s t e r ) f o r c o u t

54

a d d i

$v0 , $0 , 4

# p u t s 4

i n v0

which d e n o t e s we

a r e

p r i n t i n g a s t r i n g

55

s y s c a l l

# make

a

s y s c a l l

t o

system

56

57

move

$a0 , $ s 3

# p u t s

d i n t o

a r g 0

( a0 r e g i s t e r )

f o r

c o u t

58

a d d i

$v0 , $0 , 1

# p u t s 1 i n v0 t o d e n o t e we a r e p r i n t i n g an i n t

59

s y s c a l l

# make

a

s y s c a l l

t o

system

60

61

l a

$a0 , e n d l

# p u t s t h e a d d r e s s o f t h e s t r i n g e n d l i n t o a0

62

a d d i

$v0 , $0 , 4

# p u t s 4

i n t o v0 s a y i n g we a r e p r i n t i n g a s t r i n g

63

s y s c a l l

64

65

a d d i

$v0 , $0 , 10

66

s y s c a l l

p3codeBase.s

3

C++ Equivalent

  • #i n c l u d e <i o s t r e a m >

2

3 u s i n g namespace s t d ;

4

5

6

7 i n t main ( v o i d )

  • f

9

10

i n t

a

=

5 ;

11

i n t

b

=

6 ;

12

i n t

c

=

7 ;

13

i n t

d ;

14

15

d =

1;

16

( a < 1 0 ) f

17

i f

18

a++;

19

g e l s e f

  1. a ;

21

g

22

23

d = a + c ;

24

c = a + d ;

25

< 10 ) f

26

i f ( b

27

b++;

28

c

;

29

g e l s e f

30

b

;

31

c++;

  1. g

33

34

a =

c

+ b ;

35

b =

c

+ d ;

36

i f ( b < c && b > a ) f

37

  1. d = a + b ;

39

g e l s e i f ( b > c j j c < a ) f

40

d = b + c ;

41

g

42

43

c o u t << “Value o f a : “ << a << e n d l ;

  1. c o u t << “Value o f b : “ << b << e n d l ;

45

c o u t << “Value

o f

c :

<< c << e n d l ;

46

c o u t << “Value

o f

d :

<< d << e n d l ;

47

r e t u r n 0 ;

48

g

4

p3code.cpp

5