CS计算机代考程序代写 mips Java assembly 1

1

Week # 2 – Additional Exercises
[Solutions]

Exercise #1: Given the MIPS Instruction set, convert the following MIPS
assembly code into Java-like code. Note that “bgt” instruction is for “branch
greater than” and is used to jump to a label, if a register value is more than the
provided constant. You are expected to consider the following register
assignment while writing Java code.

MIPS Register Java Variable

$1 i

$2 tmp

$3 sum

MIPS Assembly Code

Solution in Java

and $1,$1,$0
and $2,$2,$0
and $3,$3,$0
label:
bgt $1,14,exit
multi $2,$1,2
add $3,$3,$2
addi $1,$1,1
j label
exit:

int tmp = 0;
int sum = 0;
for (int i = 0; i < 15; i++){ tmp = i * 2; sum = sum + tmp; } 2 3 Exercise #2: Given the following MIPS Assembly Instructions, write an assembly language version of the following Java code segment: int A[100], B[100], C[100]; for (i=0; i < 100; i++) { C[i] = A[i] + B[i]; } 4 At the beginning of this code segment, the only values in registers are the base addresses of arrays A, B and C in registers $0, $1 and $2. Please note that each memory location stores 4 bytes. (for this exercise, let’s assume that register $0 could be used for storing any number). Java Solution in MIPS Assembly Language int A[100], B[100], C[100]; for (i=0; i < 100; i++) { C[i] = A[i] + B[i]; } andi $3, $3, 0 # Starting index of i in register $3 ori $4, $3, 100 # Loop bound in register $4 loop: lw $5, 0($0) # Load A[i] lw $6, 0($1) # Load B[i] add $7, $5, $6 # A[i] + B[i] sw $7, 0($2) # C[i] = A[i] + B[i] addi $0, $0, 4 # Go to i+1 for A addi $1, $1, 4 # Go to i+1 for B addi $2, $2, 4 # Go to i+1 for C addi $3, $3, 1 # Increment index variable (i in the loop) bne $3, $4, loop # Compare with Loop Bound exit: Exercise #3: Using the Little Man Computer Simulator (http://peterhigginson.co.uk/LMC/), write LMC assembly programs to compute the following expressions: • e = (a+b)–(c+d) • z = 3x + y • c = a2 + b2 Solution for e = (a+b)–(c+d) // Input A & B and compute A+B; Store it in Variable AB INP STA A INP STA B ADD A STA AB OUT // Input C & D and compute C+D; Store it in Variable CD INP STA C INP STA D ADD C STA CD OUT // Load A+B from variable AB and subtract C+D from it. LDA AB SUB CD OUT HLT // Declare Variables A DAT B DAT AB DAT C DAT D DAT CD DAT // Input four numbers. // Output E = (A+B) - (C+D) - 5 - http://peterhigginson.co.uk/LMC/ Solution for z = 3x + y // Input X & Y and store in memory INP STA X INP STA Y // Load X; compute 3x and then add Y LDA X ADD X ADD X ADD Y OUT HLT // Declare Variables X DAT Y DAT // Input two numbers and compute the following // Output Z = 3X + Y Solution for c = a2 + b2 // Input A & B and store in memory INP STA A INP STA B // Prepare for A Square LDA ZERO STA A2 STA COUNT // Compute A2 LOOPA LDA A2 ADD A STA A2 LDA COUNT ADD ONE STA COUNT SUB A BRP DONEA BRA LOOPA // Print A2 DONEA LDA A2 OUT - 6 - // Prepare for B Square LDA ZERO STA B2 STA COUNT // Compute B2 LOOPB LDA B2 ADD B STA B2 LDA COUNT ADD ONE STA COUNT SUB B BRP DONEB BRA LOOPB // Print B2 DONEB LDA B2 OUT // Compute A2 + B2 and print ADD A2 OUT HLT // Declare Variables A DAT A2 DAT B DAT B2 DAT ZERO DAT 000 ONE DAT 001 COUNT DAT // Input two numbers and compute the following // Output C = A2 + B2 - 7 -