a. Write MIPS assembly code for the following machine code. Assume that the segment is placed starting at location 8000. Create labels for branch and jump instructions and indicate the
0001 0001 0010 1000 0000 0000 0000 0011
1000 1110 0001 0001 0000 0000 0000 1000
0010 0010 0010 1000 0000 0000 0000 0110
0000 1000 0000 0000 0000 0001 0000 0010
b. Translate the following C function into MIPS assembly code.
int f1(int ar1[], int ar2[], int n) {
int c = 0;
for (int i = 0; i < n; i++) {
if (ar2[i] > 0) {
ar1[i] = ar1[i] + 5;
c++;
}
}
return c;
}
Here is your solution:
Answer I: Machine code to MIPS assembly code
.BYTE 0x00, 0x01, 0x00, 0x01
.BYTE 0x00, 0x10, 0x10, 0x00
.BYTE 0x00, 0x00, 0x00, 0x00
ADD w0, w0, #0
ADR x16, #0x22010
.BYTE 0x00, 0x01, 0x00, 0x01
.BYTE 0x00, 0x00, 0x00, 0x00
.BYTE 0x00, 0x00, 0x10, 0x00
ADR x0, #0x220
.BYTE 0x00, 0x10, 0x10, 0x00
.BYTE 0x00, 0x00, 0x00, 0x00
ADR x0, #0x202c
.BYTE 0x00, 0x00, 0x10, 0x00
.BYTE 0x00, 0x00, 0x00, 0x00
.BYTE 0x00, 0x00, 0x00, 0x01
ADR x0, #0x3c
Answer 2:
C program to MIPS assembly code.
f1:
daddiu $sp,$sp,-64
sd $fp,56($sp)
move $fp,$sp
sd $4,16($fp)
sd $5,24($fp)
move $2,$6
sll $2,$2,0
sw $2,32($fp)
sw $0,0($fp)
sw $0,4($fp)
b .L2
nop
.L4:
lw $2,4($fp)
dsll $2,$2,2
ld $3,24($fp)
daddu $2,$3,$2
lw $2,0($2)
blez $2,.L3
nop
lw $2,4($fp)
dsll $2,$2,2
ld $3,16($fp)
daddu $2,$3,$2
lw $3,4($fp)
dsll $3,$3,2
ld $4,16($fp)
daddu $3,$4,$3
lw $3,0($3)
addiu $3,$3,5
sw $3,0($2)
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L3:
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
.L2:
lw $3,4($fp)
lw $2,32($fp)
slt $2,$3,$2
bne $2,$0,.L4
nop
lw $2,0($fp)
move $sp,$fp
ld $fp,56($sp)
daddiu $sp,$sp,64
j $31
nop
// happy coding
Get Answers For Free
Most questions answered within 1 hours.