$24
Description
For project four, 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, it can be done in the following steps:
Assign variables to registers. When inspecting code, any constant values in expressions may need to be assigned to temporary registers.
Initialize variables to registers. (actually put the values into the registers.)
3 Then you may begin converting 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:
Sum: 7179
Reversed Number: 98654
is Palindrome: 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
.
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
s u m l b l :
. a s c i i z
"Sum: "
#
l a b e l
f o r
sum
4
r e v l b l :
. a s c i i z
" Reversed
Number :
"
# l a b e l f o r r e v
5
p a l l b l :
. a s c i i z
" I s
Palindrome :
" #
l a b e l f o r i s P a l i n d r o m e
6
sumarr :
. word
1
7
. word
3
8
. word
44
9
. word
66
10
. word
88
11
. word
90
12
. word
9
13
. word
232
14
. word
4325
15
. word
2321
16
a r r :
. word
1
17
. word
2
18
. word
3
19
. word
4
20
. word
5
21
. word
4
22
. word
3
23
. word
2
24
. word
1
25
26
. t e x t
27
28
# sum
$ s 0
29
#
a d r e s s o f
sumarr
$ s 1
30
#
r e v
$ s 2
31
# num
$ s 3
32
# i s P a l i n d r o m e
$ s 4
33
# a d d r e s s o f a r r
$ s 5
34
#
i
$ t 0
35
# beg
$ s 6
36
# end
$ s 7
37
# d
$ t 1
38
# 10
$ t 2
39
# 100
$ t 3
40
main :
41
42
e x i t :
43
l a
$a0 , s u m l b l
# p u t s s u m 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
44
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
45
s y s c a l l
# make
a s y s c a l l
t o
system
46
47
move
$a0 ,
$ s 0
# p u t s
sum
i n t o a r g 0
( a0 r e g i s t e r )
f o r c o u t
48
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
49
s y s c a l l
# make
a s y s c a l l
t o
system
2
50
51
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
52
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
53
s y s c a l l
54
55
l a
$a0 , r e v l b l
# p u t s r e v 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
56
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
57
s y s c a l l
# make
a
s y s c a l l
t o
system
58
59
move
$a0 , $ s 1
# p u t s
r e v i n t o
a r g 0
( a0 r e g i s t e r )
f o r c o u t
60
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
61
s y s c a l l
# make
a
s y s c a l l
t o
system
62
63
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
64
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
65
s y s c a l l
66
67
l a
$a0 , p a l l b l
# p u t s p a l 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
68
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
69
s y s c a l l
# make
a
s y s c a l l
t o
system
70
71
move
$a0 , $ s 3
# p u t s i s P a l i n d r o m e
i n t o a r g 0 ( a0 r e g i s t e r )
f o r
c o u t
72
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
73
s y s c a l l
# make
a
s y s c a l l
t o
system
74
75
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
76
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
77
s y s c a l l
78
79
80
a d d i $v0 , $0 , 10
81
s y s c a l l
p4codeBase.s
3
C++ Equivalent
1
#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 )
8
f
9
10
i n t
sum =
0 ;
11
i n t s i z e = 1 0 ;
12
i n t
sumarr [ ]
= f1 , 3 , 4 4 , 6 6 , 8 8 , 9 0 , 9 , 2 3 2 , 4 3 2 5 , 2 3 2 1 g;
13
f o r ( i n t
i
=
0 ; i < s i z e ; i ++)f
14
g
sum = sum + sumarr [ i ] ;
15
16
17
i n t
num = 4 5 6 8 9 ;
18
i n t
r e v = 0 ;
19
i n t
d =
1;
20
w h i l e ( num 0 ) f
21
d = num % 1 0 ;
22
r e v = r e v 10 + d ;
23
g
num = num / 1 0 ;
24
25
i n t a r r [ ] = f1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 g;
26
27
i n t
beg
=
0 ;
28
i n t
end
=
8 ;
29
i n t i s P a l i n d r o m e = 1 ;
30
w h i l e ( beg
< end ) f
31
i f
( a r r [ beg ] != a r r [ end ] ) f
32
i s P a l i n d r o m e = 1;
33
g
break ;
34
35
beg++;
36
end
;
37
g
38
39
40
41
c o u t
<< "Sum: " << sum << e n d l ;
42
c o u t << " Reversed Number : " << r e v << e n d l ;
43
c o u t
<< " I s
Palindrome : " << i s P a l i n d r o m e << e n d l ;
44
r e t u r n
0 ;
45
g
p4code.cpp
4