Convert to MIPS assembly code #include<stdio.h> int find42(int array[],int size)
ID: 3581413 • Letter: C
Question
Convert to MIPS assembly code
#include<stdio.h>
int find42(int array[],int size);
int main()
{
int array[100],size,i;
printf("Enter the size of the array : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&array[i]);
}
printf("The index value is(index starts from 0) %d",find42(array,size));
return 0;
}
int find42(int array[100],int size)
{
int i,index,flag=0;
for(i=0;i<size;i++)
{
if(array[i]==42)
{
index=i;
flag=1;
break;
}
}
if(flag==1)
return index;
else
return -1;
}
Explanation / Answer
.file 1 ""
.section .mdebug.abi32
.previous
.gnu_attribute 4, 1
.abicalls
.rdata
.align 2
$LC0:
.ascii "Enter the size of the array :"
.align 2
$LC1:
.ascii "%d"
.align 2
$LC2:
.ascii "The index value is(index starts from 0) %d"
.text
.align 2
.globl main
$LFB0 = .
.set nomips16
.ent main
.type main, @function
main:
.frame $fp,440,$31 # vars= 408, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-440
$LCFI0:
sw $31,436($sp)
$LCFI1:
sw $fp,432($sp)
movz $31,$31,$0
$LCFI2:
move $fp,$sp
$LCFI3:
.cprestore 16
lw $2,%got($LC0)($28)
nop
addiu $4,$2,%lo($LC0)
lw $2,%call16(puts)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
addiu $2,$fp,28
lw $3,%got($LC1)($28)
nop
addiu $4,$3,%lo($LC1)
move $5,$2
lw $2,%call16(scanf)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
sw $0,24($fp)
b $L2
nop
$L3:
lw $2,24($fp)
addiu $3,$fp,32
sll $2,$2,2
addu $2,$3,$2
lw $3,%got($LC1)($28)
nop
addiu $4,$3,%lo($LC1)
move $5,$2
lw $2,%call16(scanf)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
lw $2,24($fp)
nop
addiu $2,$2,1
sw $2,24($fp)
$L2:
lw $2,28($fp)
lw $3,24($fp)
nop
slt $2,$3,$2
andi $2,$2,0x00ff
bne $2,$0,$L3
nop
lw $2,28($fp)
addiu $3,$fp,32
move $4,$3
move $5,$2
lw $2,%got(_Z6find42Pii)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
nop
lw $3,%got($LC2)($28)
nop
addiu $4,$3,%lo($LC2)
move $5,$2
lw $2,%call16(printf)($28)
nop
move $25,$2
jalr $25
nop
lw $28,16($fp)
move $2,$0
move $sp,$fp
lw $31,436($sp)
lw $fp,432($sp)
addiu $sp,$sp,440
j $31
nop
.set macro
.set reorder
.end main
$LFE0:
.size main, .-main
.align 2
.globl _Z6find42Pii
$LFB1 = .
.set nomips16
.ent _Z6find42Pii
.type _Z6find42Pii, @function
_Z6find42Pii:
.frame $fp,32,$31 # vars= 16, regs= 1/0, args= 0, gp= 8
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-32
$LCFI4:
sw $fp,28($sp)
$LCFI5:
move $fp,$sp
movz $31,$31,$0
$LCFI6:
sw $4,32($fp)
sw $5,36($fp)
sw $0,8($fp)
sw $0,16($fp)
b $L6
nop
$L9:
lw $2,16($fp)
nop
sll $2,$2,2
lw $3,32($fp)
nop
addu $2,$3,$2
lw $3,0($2)
li $2,42 # 0x2a
bne $3,$2,$L7
nop
lw $2,16($fp)
nop
sw $2,12($fp)
li $2,1 # 0x1
sw $2,8($fp)
b $L8
nop
$L7:
lw $2,16($fp)
nop
addiu $2,$2,1
sw $2,16($fp)
$L6:
lw $3,16($fp)
lw $2,36($fp)
nop
slt $2,$3,$2
andi $2,$2,0x00ff
bne $2,$0,$L9
nop
$L8:
lw $3,8($fp)
li $2,1 # 0x1
bne $3,$2,$L10
nop
lw $2,12($fp)
b $L11
nop
$L10:
li $2,-1 # 0xffffffffffffffff
$L11:
move $sp,$fp
lw $fp,28($sp)
addiu $sp,$sp,32
j $31
nop
.set macro
.set reorder
.end _Z6find42Pii
$LFE1:
.size _Z6find42Pii, .-_Z6find42Pii
.section .eh_frame,"aw",@progbits
$Lframe1:
.4byte $LECIE1-$LSCIE1
$LSCIE1:
.4byte 0x0
.byte 0x1
.globl __gxx_personality_v0
.ascii "zP"
.uleb128 0x1
.sleb128 -4
.byte 0x1f
.uleb128 0x5
.byte 0x0
.4byte __gxx_personality_v0
.byte 0xc
.uleb128 0x1d
.uleb128 0x0
.align 2
$LECIE1:
$LSFDE1:
.4byte $LEFDE1-$LASFDE1
$LASFDE1:
.4byte $LASFDE1-$Lframe1
.4byte $LFB0
.4byte $LFE0-$LFB0
.uleb128 0x0
.byte 0x4
.4byte $LCFI0-$LFB0
.byte 0xe
.uleb128 0x1b8
.byte 0x4
.4byte $LCFI2-$LCFI0
.byte 0x11
.uleb128 0x1e
.sleb128 2
.byte 0x11
.uleb128 0x1f
.sleb128 1
.byte 0x4
.4byte $LCFI3-$LCFI2
.byte 0xd
.uleb128 0x1e
.align 2
$LEFDE1:
.ident "GCC: (Debian 4.4.5-8) 4.4.5"