Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following program from machine language into MIPS assembly language.

ID: 2084346 • Letter: C

Question

Convert the following program from machine language into MIPS assembly language. The numbers on the left are the instruction address in memory, and the numbers on the right give the instruction at that address. Then reverse engineer a high-level program that would compile into this assembly language routine and write it. Explain in words what the program does. $a0 is the input, and it initially contains a positive number, n. $v0 is the output.

0x00400000 0x20080000

0x00400004 0x20090001

0x00400008 0x0089502a

0x0040000c 0x15400003

0x00400010 0x01094020

0x00400014 0x21290002

0x00400018 0x08100002

0x0040001c 0x01001020

Explanation / Answer

·        First instruction: The binary illustration of 0x20080000 is given by
0010 0000 0000 an 0000 0000 0000 0000.
The cluster of left six bits (001000) corresponds to the op code addi.
Following is that the methodology of attack for obtaining the corresponding assembly instruction.

·        Binary illustration within the machine format: 001000 00000 01000 0000000000000000
Binary illustration within the assembly format: 001000 01000 00000 0000000000000000
Assembly instruction addi $t0 $0 0
Final answer: addi $t0, $0, 0

·        Second instruction: The binary illustration of 0x20090001 is given by
0010 0000 0000 1001 0000 0000 0000 0001.
Again, the cluster of left six bits (001000) corresponds to the op code addi.

·        Binary illustration within the machine format: 001000 00000 01001 0000000000000001
Binary illustration within the assembly format: 001000 01001 00000 0000000000000001
Assembly instruction addi $t1 $0 1
Final answer: addi $t1, $0, 1

·        Third instruction: The binary illustration of 0x0089502a is given by
0000 0000 1000 1001 0101 0000 0010 1010.
The cluster of left six bits (i.e., 000000) indicates that an R-type instruction that,
in turn, suggests that we have a tendency to should examine the cluster of last six bits (i.e., 101010) for funct.
It seems that 101010 corresponds to slt.

·        Binary illustration within the machine format: 000000 00100 01001 01010 00000 101010
Binary illustration within the assembly format: 000000 01010 00100 01001 00000 101010
Assembly instruction slt $t2 $a0 $t1 -- --
Final answer: slt $t2, $a0, $t1

·        Fourth instruction: The binary illustration of 0x15400003 is given by
0001 0101 0100 0000 0000 0000 0000 0011.
The cluster of left six bits (i.e., 000101) corresponds to the op code bne.
bne Rs Rt Imm

·        Binary illustration within the machine format: 000101 01010 00000 0000000000000011
At now, recall the programming language format of this instruction and its
correspondence with the machine language format:
bne Rs, Rt, Label #If [Rs]!=[Rt] then pc = pc + se Imm << 2
In this explicit case, Imm << 2 in binary is given by 0000000000001100 that's adequate
0x0000000c. Further, computer presently holds 0x00400010 (address of future instruction).
Accordingly, Label itself can correspond to the memory address 0x00400010 and
0x0000000c that's 0x0040001c. Let L2 denote the address 0x0040001c.
Binary illustration within the assembly format: 000101 01010 00000 0000000000011100
Assembly instruction bne $t2 $0 L2
Final answer: bne $t2, $0, L2

·        Fifth instruction: The binary illustration of 0x01094020 is given by
0000 0001 0000 1001 0100 0000 0010 0000.
The cluster of left six bits (i.e., 000000) indicates that it's an R-type instruction that,
in turn, suggests that we have a tendency to should examine the cluster of last six bits (i.e., 100000) for funct.
It seems that 100000 corresponds to feature.

·        Binary illustration within the machine format: 000000 01000 01001 01000 00000 100000
Binary illustration within the assembly format: 000000 01000 01000 01001 00000 100000
Assembly instruction add $t0 $t0 $t1 -- --
Final answer: add $t0, $t0, $t1

·        Sixth instruction: The binary illustration of 0x21290002 is given by
0010 0001 0010 1001 0000 0000 0000 0010.
The cluster of left six bits (001000) corresponds to the op code addi.

·        Binary illustration within the machine format: 001000 01001 01001 0000000000000010
Binary illustration within the assembly format: 001000 01001 01001 0000000000000010
Assembly instruction addi $t1 $t1 2
Final answer: addi $t1, $t1, 2

·        Seventh instruction: The binary illustration of 0x08100002 is given by
0000 1000 0001 0000 0000 0000 0000 0010.
The cluster of left six bits (i.e., 000010) corresponds to the op code j for jump.
j Imm

·        Binary illustration within the machine format: 000010 00000100000000000000000010
Load the computer with the address e by concatenating the primary four bits of the present computer
with the worth within the immediate field cushioned at the right finish by two 0s. Now, PC
currently holds 0x0040001c (address of future instruction). consequently, jump are
made to the memory address 00000000010000000000000000001000 that's 0x00400008.
Let L1 denote this address.
Final answer: j L1

·        Eighth instruction: The binary illustration of 0x01001020 is given by
0000 0001 0000 0000 0001 0000 0010 0000.
The cluster of left six bits (i.e., 000000) indicates that it's an R-type instruction that,
in turn, suggests that we have a tendency to should examine the cluster of last six bits (i.e., 100000) for funct.
It seems that 100000 corresponds to feature.

·        Binary illustration within the machine format: 000000 01000 00000 00010 00000 100000
Binary illustration within the assembly format: 000000 00010 01000 00000 00000 100000
Assembly instruction add $v0 $t0 $0 -- --
Final answer: add $t0, $t0, $t1
Here is that the complete assembly code similar to the given machine code:
    addi $t0, $0, 0
    addi $t1, $0, 1
    slt $t2, $a0, $t1
L1: bne $t2, $0, L2
     add $t0, $t0, $t1
     addi $t1, $t1, 2
     j L1
L2: add $t0, $t0, $t1