build your own assembler that reads the R/I/J Format Instructions of MIPS I-Form
ID: 3887504 • Letter: B
Question
build your own assembler that reads the R/I/J Format Instructions of MIPS
I-Format (Data transfer, branch format)
op rs rt address 6 bits 5 bits 5 bits 16 bits
J-Format (Jump instruction format)
op address 6 bits 26 bits
R-Format (Arithmetic instruction format)
op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
using the template below:
#include <stdio.h>
#define MAXLINE 80
#define MAXREG 5
int main()
{
char line[MAXLINE]={0};
char oper[MAXLINE];
char rd[MAXREG],rs[MAXREG],rt[MAXREG];
while (fgets(line, MAXLINE, stdin)) {
/* check if a 3-address R format instruction */
if (sscanf(line, "%s $%[^,],$%[^,],$%s", oper, rd, rs, rt) == 4) {
printf("input line: %s ", line);
printf("parsed line: op:%10s rd:%5s rs:%5s rt:%5s ",
oper, rd, rs, rt);
}
/* you need to add other patterns for R,I,and J encoding */
else {
printf("input line: %s ", line);
printf("you need to add sscanf format ");
}
}
return 0;
}
Explanation / Answer
MIPS code:-
$LFB0 = .
main:
addiu $sp,$sp,-224
sw $31,220($sp)
sw $fp,216($sp)
move $fp,$sp
addiu $2,$fp,32
li $3,80 # 0x50
move $6,$3
move $5,$0
move $4,$2
jal memset
nop
lui $2,%hi(stdin)
lw $2,%lo(stdin)($2)
nop
move $6,$2
li $5,80 # 0x50
addiu $2,$fp,32
move $4,$2
jal fgets
nop
sltu $2,$0,$2
andi $2,$2,0x00ff
beq $2,$0,$L2
nop
addiu $4,$fp,192
addiu $3,$fp,112
addiu $2,$fp,208
sw $2,20($sp)
addiu $2,$fp,200
sw $2,16($sp)
move $7,$4
move $6,$3
lui $2,%hi($LC0)
addiu $5,$2,%lo($LC0)
addiu $2,$fp,32
move $4,$2
jal sscanf
nop
xori $2,$2,0x4
sltu $2,$2,1
andi $2,$2,0x00ff
beq $2,$0,$L3
nop
addiu $2,$fp,32
move $5,$2
lui $2,%hi($LC1)
addiu $4,$2,%lo($LC1)
jal printf
nop
addiu $5,$fp,200
addiu $4,$fp,192
addiu $3,$fp,112
addiu $2,$fp,208
sw $2,16($sp)
move $7,$5
move $6,$4
move $5,$3
lui $2,%hi($LC2)
addiu $4,$2,%lo($LC2)
jal printf
nop
b $L5
nop
addiu $2,$fp,32
move $5,$2
lui $2,%hi($LC1)
addiu $4,$2,%lo($LC1)
jal printf
nop
lui $2,%hi($LC3)
addiu $4,$2,%lo($LC3)
jal puts
nop
b $L5
nop
move $2,$0
move $sp,$fp
lw $31,220($sp)
lw $fp,216($sp)
addiu $sp,$sp,224
j $31
nop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.