1
Suppose that i
and k
correspond to the registers $s3
and $s5
, and the basis of array save
is in $s6
.
What code Assembly MIPS corresponding to that code segment C?
while( s a v e [ i ] == k )
i += 1 ;
1
Suppose that i
and k
correspond to the registers $s3
and $s5
, and the basis of array save
is in $s6
.
What code Assembly MIPS corresponding to that code segment C?
while( s a v e [ i ] == k )
i += 1 ;
2
You can use the GNU Compiler Collection to generate the code Assembly equivalent to the program originally written in language C
.
Assuming you’re in an architecture x86
, you will need a version of GCC
capable of generating machine code for the architectures MIPS
(cross-pilot), in the case in question we will use the mips64-linux-gnu-gcc
.
Installing the GCC
in distros Redhat (yum):
# yum install gcc-mips64-linux-gnu
Installing the GCC
in distros Debian (apt-get):
# apt-get install gcc-mips64-linux-gnu
First we implemented a program containing the language code segment C
which will be analyzed, let’s call it teste.c
:
int main( void )
{
int i = 0;
int k = 1;
int save[ 8 ] = { 8, 7, 6, 5, 4, 3, 2, 1 };
while( save[ i ] == k )
i += 1;
return 0;
}
To generate the code in Assembly for architecture MIPS
equivalent to the program teste.c
listed above:
$ mips64-linux-gnu-gcc -S teste.c -o teste.asm
A file of name teste.asm
will be generated:
.file 1 "teste.c"
.section .mdebug.abiN32
.previous
.gnu_attribute 4, 1
.abicalls
.rdata
.align 3
.LC0:
.word 8
.word 7
.word 6
.word 5
.word 4
.word 3
.word 2
.word 1
.text
.align 2
.globl main
.set nomips16
.ent main
.type main, @function
main:
.frame $fp,64,$31 # vars= 48, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-8
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-64
sd $fp,56($sp)
move $fp,$sp
sw $0,0($fp)
li $2,1 # 0x1
sw $2,4($fp)
lui $2,%hi(.LC0)
addiu $3,$2,%lo(.LC0)
ldl $4,%lo(.LC0)($2)
move $5,$4
ldr $5,7($3)
addiu $3,$2,%lo(.LC0)
ldl $4,8($3)
ldr $4,15($3)
addiu $3,$2,%lo(.LC0)
ldl $6,16($3)
move $7,$6
ldr $7,23($3)
move $3,$7
addiu $2,$2,%lo(.LC0)
ldl $6,24($2)
move $7,$6
ldr $7,31($2)
move $2,$7
sd $5,8($fp)
sd $4,16($fp)
sd $3,24($fp)
sd $2,32($fp)
.option pic0
j .L2
nop
.option pic2
.L3:
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
.L2:
lw $2,0($fp)
sll $2,$2,2
addu $2,$fp,$2
lw $3,8($2)
lw $2,4($fp)
beq $3,$2,.L3
nop
move $2,$0
move $sp,$fp
ld $fp,56($sp)
addiu $sp,$sp,64
j $31
nop
.set macro
.set reorder
.end main
.size main, .-main
.ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-16)"
With a brief review of the code Assembly generated, we finally arrived at the code segment equivalent to the loop while
:
j .L2
nop
.option pic2
.L3:
lw $2,0($fp)
addiu $2,$2,1 # i += 1;
sw $2,0($fp)
.L2:
lw $2,0($fp)
sll $2,$2,2
addu $2,$fp,$2
lw $3,8($2)
lw $2,4($fp)
beq $3,$2,.L3 # while( save[ i ] == k )
nop
Browser other questions tagged c assembly mips
You are not signed in. Login or sign up in order to post.