Consider the following C code snippet C code void setArrayi
Consider the following C code snippet:
// C code
void setArray(int num) {
int i;
int array[10];
for (i=0; i < 10; i=i+1) {
array[i]=compare(num, i);
}
}
int compare(int a, int b) {
if (sub(a, b) >= 0)
return 1;
else
return 0;
}
int sub(int a, int b) {
return ab;
}
Implement the C code snippet above in MIPS assembly language. Use $s0 to hold the variable i. Be sure to handle the stack pointer appropriately. The array is stored on the stack of the setArray function (i.e. when setArray function is called it allocates space for the array on the stack.).
Solution
I have used the Test function i.e ain function to get this module of the fuction definition working.
Thw below is the MIPS code for the complete program:-
.file 1 \"\"
.section .mdebug.abi32
.previous
.gnu_attribute 4, 1
.abicalls
.text
.align 2
.globl _Z3subii
$LFB0 = .
.set nomips16
.ent _Z3subii
.type _Z3subii, @function
_Z3subii:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
$LCFI0:
sw $fp,4($sp)
$LCFI1:
move $fp,$sp
movz $31,$31,$0
$LCFI2:
sw $4,8($fp)
sw $5,12($fp)
lw $3,8($fp)
lw $2,12($fp)
nop
subu $2,$3,$2
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end _Z3subii
$LFE0:
.size _Z3subii, .-_Z3subii
.align 2
.globl _Z7compareii
$LFB1 = .
.set nomips16
.ent _Z7compareii
.type _Z7compareii, @function
_Z7compareii:
.frame $fp,32,$31 # vars= 0, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-32
$LCFI3:
sw $31,28($sp)
$LCFI4:
sw $fp,24($sp)
movz $31,$31,$0
$LCFI5:
move $fp,$sp
$LCFI6:
.cprestore 16
sw $4,32($fp)
sw $5,36($fp)
lw $4,32($fp)
lw $5,36($fp)
lw $2,%got(_Z3subii)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
nor $2,$0,$2
srl $2,$2,31
andi $2,$2,0x00ff
beq $2,$0,$L4
nop
li $2,1 # 0x1
b $L5
nop
$L4:
move $2,$0
$L5:
move $sp,$fp
lw $31,28($sp)
lw $fp,24($sp)
addiu $sp,$sp,32
j $31
nop
.set macro
.set reorder
.end _Z7compareii
$LFE1:
.size _Z7compareii, .-_Z7compareii
.align 2
.globl _Z8setArrayi
$LFB2 = .
.set nomips16
.ent _Z8setArrayi
.type _Z8setArrayi, @function
_Z8setArrayi:
.frame $fp,88,$31 # vars= 48, regs= 3/0, args= 16, gp= 8
.mask 0xc0010000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-88
$LCFI7:
sw $31,84($sp)
$LCFI8:
sw $fp,80($sp)
movz $31,$31,$0
$LCFI9:
sw $16,76($sp)
$LCFI10:
move $fp,$sp
$LCFI11:
.cprestore 16
sw $4,88($fp)
sw $0,24($fp)
b $L8
nop
$L9:
lw $16,24($fp)
lw $4,88($fp)
lw $5,24($fp)
lw $2,%got(_Z7compareii)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
move $3,$2
sll $2,$16,2
addiu $4,$fp,24
addu $2,$4,$2
sw $3,4($2)
lw $2,24($fp)
nop
addiu $2,$2,1
sw $2,24($fp)
$L8:
lw $2,24($fp)
nop
slt $2,$2,10
andi $2,$2,0x00ff
bne $2,$0,$L9
nop
move $sp,$fp
lw $31,84($sp)
lw $fp,80($sp)
lw $16,76($sp)
addiu $sp,$sp,88
j $31
nop
.set macro
.set reorder
.end _Z8setArrayi
$LFE2:
.size _Z8setArrayi, .-_Z8setArrayi
.align 2
.globl main
$LFB3 = .
.set nomips16
.ent main
.type main, @function
main:
.frame $fp,48,$31 # vars= 16, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-48
$LCFI12:
sw $31,44($sp)
$LCFI13:
sw $fp,40($sp)
movz $31,$31,$0
$LCFI14:
move $fp,$sp
$LCFI15:
.cprestore 16
li $2,5 # 0x5
sw $2,32($fp)
li $2,10 # 0xa
sw $2,28($fp)
li $2,8 # 0x8
sw $2,24($fp)
li $4,5 # 0x5
lw $2,%got(_Z8setArrayi)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
lw $4,28($fp)
lw $5,24($fp)
lw $2,%got(_Z7compareii)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
lw $4,28($fp)
lw $5,24($fp)
lw $2,%got(_Z3subii)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
move $2,$0
move $sp,$fp
lw $31,44($sp)
lw $fp,40($sp)
addiu $sp,$sp,48
j $31
nop
.set macro
.set reorder
.end main
![Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int](/WebImages/31/consider-the-following-c-code-snippet-c-code-void-setarrayi-1089051-1761573122-0.webp)
![Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int](/WebImages/31/consider-the-following-c-code-snippet-c-code-void-setarrayi-1089051-1761573122-1.webp)
![Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int](/WebImages/31/consider-the-following-c-code-snippet-c-code-void-setarrayi-1089051-1761573122-2.webp)
![Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int](/WebImages/31/consider-the-following-c-code-snippet-c-code-void-setarrayi-1089051-1761573122-3.webp)
![Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int Consider the following C code snippet: // C code void setArray(int num) { int i; int array[10]; for (i=0; i < 10; i=i+1) { array[i]=compare(num, i); } } int](/WebImages/31/consider-the-following-c-code-snippet-c-code-void-setarrayi-1089051-1761573122-4.webp)