Project 5 Solution

$35.00

Category: Tag:

Description

Description

For project ve, 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 sumOfDoubleEvenPlace: and getDigit: labels.

When performing a C++ to MIPS conversion with functions, do so in the following steps:

1 Assign variables to registers. When inspecting the C++ code, any constant val-ues(literals) may need to be assigned to temporary registers. Please check the com-ments in the .s le to see a list of pre-assigned variables to registers.

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

3 Then move onto the rest of the code.

  • For functions, remember for non-leaf functions(functions that call other functions, you must saved the values of certain registers you are using to ensure correct code execution. You will save these values on the stack. Pushing values is done via storeword(sw) and popping values is done via loadword(lw).

  • Remember that $t, $v, $a, and $ra registers are assumed not to be preserved across function calls.

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:

Expected Value: 23 Value: 23

Expected Value: 21 Value: 21

1

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

Base MIPS Code

  • . data

2

expVal23 :

. a s c i i z

“Expected

Value

:

23

Your

Value

:

3

expVal21 :

. a s c i i z

“Expected

Value

:

21

Your

Value

:

4

e n d l :

. a s c i i z

“nn”

5

6

. t e x t

7

8

# #

9

# i n t

g e t D i g i t ( i n t number ) ;

10

# L i s t

Used R e g i s t e r s Here :

  1. #

  1. # #

  1. g e t D i g i t :

14

15

16

  1. ##

18

#

i n t sumOfDoubleEvenPlace ( i n t number ) ;

19

#

L i s t

Used R e g i s t e r s Here :

20

# sum

> $ s 0

21 # d i g i t

> $ s 1

  1. #

  1. ##

  1. sumOfDoubleEvenPlace :

25

  1. main :

27

l i

$s0 ,

89744563

#

i n t

t e s t 1

= 89744563;

28

l i

$s1 ,

98756421

#

i n t

t e s t 2

= 98756421;

29

l i $s2 , 0

# i n t r e s u l t 1

= 0 ;

30

l i $s3 , 0

# i n t r e s u l t 2

= 0 ;

31

32

33

#

code

f o r f i r s t

f u n c t i o n c a l l

34

  1. add $a0 , $0 , $ s 0

  1. j a l sumOfDoubleEvenPlace

  1. add $s2 , $0 , $v0

2

38

39

l a

$a0 , expVal23

  1. a d d i $v0 , $0 , 4

  1. s y s c a l l

42

43

move

$a0 ,

$ s 2

44

a d d i $v0 , $0 , 1

45

s y s c a l l

46

47

l a

$a0 ,

e n d l

  1. a d d i $v0 , $0 , 4

  1. s y s c a l l

50

51

# code f o r f i r s t f u n c t i o n c a l l

52

  1. add $a0 , $0 , $ s 1

  1. j a l sumOfDoubleEvenPlace

  1. add $s3 , $0 , $v0

56

57

l a

$a0 , expVal21

  1. a d d i $v0 , $0 , 4

  1. s y s c a l l

60

61

move

$a0 ,

$ s 3

62

a d d i $v0 , $0 , 1

63

s y s c a l l

64

65

l a

$a0 ,

e n d l

  1. a d d i $v0 , $0 , 4

  1. s y s c a l l

68

  1. l i $v0 , 10

  1. s y s c a l l

3

C++ Equivalent

  • #i n c l u d e <b i t s / s t d c ++.h>

2 u s i n g namespace s t d ;

3

4

i n t

sumOfDoubleEvenPlace ( i n t number ) ;

5

i n t

g e t D i g i t ( i n t number ) ;

6

  • i n t main ( v o i d )

  • f

9

i n t t e s t 1 = 8 9 7 4 4 5 6 3 ;

10

i n t t e s t 2 = 9 8 7 5 6 4 2 1 ;

11

i n t r e s u l t 1 = 0 ;

12

i n t r e s u l t 2 = 0 ;

13

14

r e s u l t 1 = sumOfDoubleEvenPlace ( t e s t 1 ) ;

15

c o u t << “Expected Value :

23

Value : ” << r e s u l t 1

<< e n d l ;

16

17

r e s u l t 2 = sumOfDoubleEvenPlace ( t e s t 2 ) ;

18

c o u t << “Expected Value :

21

Value : ” << r e s u l t 2

<< e n d l ;

19

  1. g

  1. /

22

Function r e t u r n s

t h e sum

o f

t h e

even p l a c e d

23

d i g i t s ( a f t e r

b e i n g doubled )

s t a r t i n g

from t h e

l e f t .

24

Note t h a t t h e

a l g o r i t h m s t a r t s

c o u n t i n g from

1 not 0 .

T h e r e f o r e ,

25

g i v e n t h e number

1234 , 4

i s

t h e

f i r s t

d i g i t from

t h e

l e f t .

26

So

t h e

even p l a c e d d i g i t s

a r e 3

and 1

and t h e

odd

p l a c e d i g i t s a r e

27

4

and 2

from

t h e

l e f t .

  1. /

29 i n t sumOfDoubleEvenPlace ( i n t number ) f

  1. i n t sum = 0 ;

  1. i n t d i g i t ;

32

33

//Remove f i r s t odd d i g i t

  1. number = number / 1 0 ;

35

  1. w h i l e ( number > 0 ) f

37 //Grab even p l a c e d d i g i t

  1. d i g i t = ( number % 1 0 ) ;

39

// Double t h e

d i g i t and

p a s s

i t

t o g e t D i g i t ,

40

//Add

r e s u l t

t o sum

41

sum +=

g e t D i g i t ( d i g i t 2 ) ;

42

//Remove c u r r e n t even

d i g i t

and

t h e next odd d i g i t .

  1. number = number / 1 0 0 ;

  1. g

45

r e t u r n

sum ;

46

g

47

/ g e t D i g i t

48

r e t u r n s t h e sum o f t h e d i g i t s i n

49

a 1 o r 2

d i g i t number .

4

50 i f number i s < 1 0 ,

  1. then we r e t u r n t h e number .

52 e l s e we r e t u r n t h e sum o f t h e d i g i t s i n t h e 2 d i g i t

  1. number .

  1. For example :

  1. 1 would r e t u r n 1

  1. 11 would r e t u r n 2

  1. 18 would r e t u r n 9

  1. /

59 i n t g e t D i g i t ( i n t number ) f

  1. i n t sum = 0 ;

61 i f ( number < 1 0 ) f

  1. sum = number ;

63 g e l s e f

  1. sum = number%10 + number / 1 0 ;

  1. g

66 r e t u r n sum ;

  1. g

5