Question: Exercise: Practice with arrays, loops, and if statements in MARS Assembly Language (need to have MARS installed) What to Do Get the file lab2exD.c. Follow

Exercise: Practice with arrays, loops, and if statements in MARS Assembly Language (need to have MARS installed)

What to Do

Get the file lab2exD.c. Follow the instructions given in comments in the file.

Hint #1: Start by making a copy of stub1.asm, and renaming it lab2exD.asm.

Exercise: Practice with arrays, loops, and if statements in MARS Assembly Language

What to provide: Code from your lab2ex.D.asm file (copy paste here and provide screenshots).

Find the given files below, names in bold.

// lab2exD.c

//

// INSTRUCTIONS

// Your overall goal is to translate this program into MARS

// assembly language. Proceed using the following steps:

//

// 1. Make sure you know what this C program does. If you're not

// sure, add calls to printf.

//

// 2. Translate the program to Goto-C. If you're not sure your

// translation is correct, add calls to printf.

//

// 3. Write down a list of local variables and the registers

// you need for them. (You will later type this in as

// a comment in your MARS source code.)

//

// 4. Using the products of Steps 2 and 3, write a MARS translation

// of this program, in a file called lab2xD.asm. Use comments (a)

// to describe allocation of local variable to s-registers and

// (b) to explain what each MARS instruction does.

//

// Your MARS code must closely match the C code. In particular,

// the translation of the for loop should use pointer

// arithmetic, and the translation of the while loop should include

// generation of the address of alpha[j] by adding 4 times j to

// the address of alpha[j], and a similar thing for beta[k].

//

// 5. Test your translation using MARS.

int alpha[8] = { 0xb1, 0xe1, 0x91, 0xc1, 0x81, 0xa1, 0xf1, 0xd1 };

int beta[8] = { 0x0, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70 };

int main(void)

{

int *p;

int *guard;

int min, j, k;

// Put value of smallest element of alpha into min.

p = alpha;

guard = p + 8;

min = *p;

for (p++; p != guard; p++) {

if (*p

min = *p;

}

// Copy elements from beta to alpha in reverse order,

// writing over the initial values in alpha.

j = 0;

k = 7;

while (j

alpha[j] = beta[k];

j++;

k--;

}

return 0;

}

# stub1.asm # This program has complete start-up and clean-up code, and a "stub" # main function.

# BEGINNING of start-up & clean-up code. Do NOT edit this code. .data exit_msg_1: .asciiz "***About to exit. main returned " exit_msg_2: .asciiz ".*** " main_rv: .word 0 .text # adjust $sp, then call main addi $t0, $zero, -32 # $t0 = 0xffffffe0 and $sp, $sp, $t0 # round $sp down to multiple of 32 jal main nop # when main is done, print its return value, then halt the program sw $v0, main_rv la $a0, exit_msg_1 addi $v0, $zero, 4 syscall nop lw $a0, main_rv addi $v0, $zero, 1 syscall nop la $a0, exit_msg_2 addi $v0, $zero, 4 syscall nop addi $v0, $zero, 10 syscall nop # END of start-up & clean-up code.

# Below is the stub for main. Edit it to give main the desired behaviour. .text .globl main main: add $v0, $zero, $zero # return value from main = 0 jr $ra

Screenshot of stub1.asm

(need to have MARS installed) What to Do Get the file lab2exD.c.

Hint #2: This is one good way to set up your two global arrays, each with eight ints: .data globl alpha alpha: Word Oxbi, Oxel, Ox91, Oxci, 0x81, Oxal, Oxf1, Oxd1 globl beta beta: .word Oxo, Ox10, Ox20, Ox30, Ox40, Ox50, Ox60, 0x70 # stubl.asm # This program has complete start-up and clean-up code, and a "stub" # main function. # BEGINNING of start-up & clean-up code. Do NOT edit this code. .data exit_msg_1: asciiz "***About to exit. main returned" exit_msg_2: .asciiz ".*** " main_rv: .word 0 .text # adjust $sp, then call main addi $t0, $zero, -32 and $sp,$sp,$t0 jal main nop # $to Oxffffffel # round $sp down to multiple of 32 SW # when main is done, print its return value, then halt the program $v0, main_rv la $a0, exit_msg_1 addi $v0, $zero, 4 syscall nop lw $a0, main_rv addi $v0, $zero, 1 syscall nop la $a0, exit_msg_2 addi $v0, $zero, 4 syscall nop addi $v0, $zero, 10 syscall nop # END of start-up & clean-up code. # Below is the stub for main. Edit it to give main the desired behaviour. .text globl main main: add $v0, $zero, $zero # return value from main = 0 jr $ra

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!