a. [ 5 points] Write the MIPS assembly code equivalent to the following HLL code
ID: 3904887 • Letter: A
Question
a. [ 5 points] Write the MIPS assembly code equivalent to the following HLL code.
Assume the variables A, B, and C are in $8, $9 and $10 respectively, and the variable ALLEQUAL is left in $11.
IF (A == B) AND (B == C)
ALLEQUAL = 1;
ELSE
ALLEQUAL= 0;
b. [5 points] Complete the following MIPS assembly code (or, you may write your own code), which is an assembly language version of the following java code segment(or, you may write your own code):
If (A[i] != A[i +1])
A[i] = 2 * A [i+1];
At the beginning of this code segment, the only values in registers are the base address of array A
in register $S0 and the value of i is in $S1.
sll $s1, $s1, 2
add $s0, $s0, $s1
lw $t1, 0($s0) # $t1 ß A[i]
lw $t2, 4($s0)
beq $t1, $t2, endIf
add $t2, $t2, $t2
sw $t2, 0($s0)
endif:
c.
.globl main
main:
addi $s0, $0, -5, # $s0 = x = -5
move $a0, $s0 # to pass the parameter
ja l ABS # call abs(x)
move $s0, $v0 # x = abs(x)
move $a0, $s0 # to print integer x
li $v0, 1
syscall
li $v0, 10 # to exit the program
syscall
ABS:
bgt $a0, $0, IF # if (x > 0) branch to label IF
sub $v0, $0, $a0 # else return -1 * x ( or 0 - x)
j ENDIF
IF:
move $v0, $a0 # return x
ENDIF:
jr $ra
[10 points] Complete the MIPS assembly language version of the given java code segment: The program converts a variable to its absolute value and display it on console.
Use $s0 for variable x.
main(String[] args) {
int x = -5;
x = abs(x);
System.out.println(x);
}
int abs(int x){
if (x > 0)
return x;
else
return (0 - x);
}
IF (A == B) AND (B == C)
ALLEQUAL = 1;
ELSE
ALLEQUAL= 0;
Explanation / Answer
ScreenShot
a) Convert the code into MIPS
IF (A == B) AND (B == C) ALLEQUAL = 1;
ELSE ALLEQUAL= 0;
MIPS:-
#Program start
.text
#if A==B where A in $8 and B in $9
beq $8,$9,next
#otherwise set ALLEQUAl=0 where ALLEQUAl in $11
li $11,0
#then exit
j exit
#If A==B then
next:
#check B==C
beq $9,$10,next1
#otherwise set ALLEQUAl=0 where ALLEQUAl in $11
li $11,0
#then exit
j exit
#If A==B and B==C then ALLEQUAl=1
next1:
li $11,1
#End of the program
exit:
li $v0,10
syscall
----------------------------------------------------------
c) Complete the MIPS assembly language version of the given java code segment: The program converts a variable to its absolute value and display it on console.
MIPS:-
.text
#x=-5
li $s0,-5
#x=abs(x)
move $a0,$s0
jal absolute
#System.out.println
move $a0,$v0
li $v0,1
syscall
#end of the program
li $v0,10
syscall
#int abs(int x)
absolute:
#if x>0
bgt $a0,0,ret
sub $a0,$0,$a0
ret:
move $v0,$a0
jr $ra
----------------------------------------------------------
b)Complete the following MIPS assembly code (or, you may write your own code), which is an assembly language version of the following java code segment(or, you may write your own code):
If (A[i] != A[i +1])
A[i] = 2 * A [i+1];
MIPS:-
#for testing just create an array with 2 values
.data
array: .word 2 3 4
#main program
.text
#Array address in s0
la $s0,array
#set i=0
addi $s1,$0,0
loop:
#check i reach array size
bge $s1,3,exit
#A[i] in t0
lw $t0,0($s0)
#A[i+1] in t1
lw $t1,4($s0)
#if(!a[1]==a[1+1]
bne $t0,$t1,if
#otherwise load value to a0 to check the correct answercheck
lw $a0,0($s0)
li $v0,1
syscall
#increment counter
addi $s1,$s1,2
#increment address of the array to get next element
addi $s0,$s0,4
#check reach the end of the array
j loop
#end of the program
exit:
li $v0,10
syscall
#if condition true
if:
#2*A[i+1]
add $t1,$t1,$t1
#store result in A[i]
sw $t1,0($s0)
#display result
lw $a0,0($s0)
li $v0,1
syscall
#increment counter
addi $s1,$s1,2
#increment address of the array to get next element
addi $s0,$s0,4
#continue loop
j loop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.