Question: Write in C++ an assembler for a subset of the MIPS instruction set. It should read the assembly file from standard input and write the

Write in C++ an assembler for a subset of the MIPS instruction set. It should read the assembly file from standard input and write the machine code to standard output. It must recognize the directives: .text , .data, .word, .space n. The assembler needs to read the I-Instruction format lw, sw, beq, and bne and identify the labels.

.text

addu $s0,$zero,$zero # s0 = 0

addu $s1,$zero,$zero # s1 = 0

addiu $v0,$zero,5 # v0 = read value

syscall

sw $v0,n($gp) # M[n] = v0

L1: lw $s2,n($gp) # s2 = M[n]

slt $t0,$s1,$s2 # if s1 >= s2 then

beq $t0,$zero,L2 # goto L2

addiu $v0,$zero,5 # v0 = read value

syscall

addu $s0,$s0,$v0 # s0 += v0

addiu $s1,$s1,1 # s1 += 1

j L1 # goto L2

L2: addu $a0,$s0,$zero # a0 = s0

addiu $v0,$zero,1 # print a0

syscall

addiu $v0,$zero,10 # exit

syscall

.data

n: .word 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!