Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assembly Language Programming in MARS(MIPS Assembler and Runtime Simulation) Please note you will need to have MARS installed for the following exercise. What to submit:

Assembly Language Programming in MARS(MIPS Assembler and Runtime Simulation)

Please note you will need to have MARS installed for the following exercise.

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

What to submit: Include the code in your COMPLETED array-sum2C.asm file (copy paste the code here and provide screenshots).

Find the required files below the names are bolded

// add-ints2C.c

int foo = 0x15;

int bar = 0x2d;

int quux;

int main(void)

{

quux = foo + bar;

return 0;

}

# add-ints2C.asm

# Start-up and clean-up code copied from stub1.asm

# 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.

# Global variables. .data .globl foo foo: .word 0x15 .globl bar bar: .word 0x2d .globl quux quux: .word 0

# Instructions for main .text .globl main main: la $t0, foo # $t0 = &foo lw $t1, ($t0) # $t1 = foo la $t2, bar # $t2 = &bar lw $t3, ($t2) # $t3 = bar add $t4, $t1, $t3 # $t4 = $t1 + $t3 la $t5, quux # $t5 = &quux sw $t4, ($t5) # quux = $t4 add $v0, $zero, $zero # return value from main = 0 jr $ra

# 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

#array-sum2C.asm

# Start-up and clean-up code copied from stub1.asm

# 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.

# Global variables .data # int abc[ ] = {-32, -8, -4, -16, -128, -64} .globl abc abc: .word -32, -8, -4, -16, -128, -64

# Hint for checking that the original program works: # The sum of the six array elements is -252, which will be represented # as 0xffffff04 in a MIPS GPR.

# Hint for checking that your final version of the program works: # The maximum of the four array elements is -4, which will be represented # as 0xfffffffc in a MIPS GPR.

# int main(void) # # local variable register # int *p $s0 # int *end $s1 # int sum $s2 # int max $s3 (to be used when students enhance the program)

.text .globl main main: la $s0, abc # p = abc addi $s1, $s0, 24 # end = p + 6 add $s2, $zero, $zero # sum = 0 L1: beq $s0, $s1, L2 # if (p == end) goto L2 lw $t9, ($s0) # $t9 = *p add $s2, $s2, $t9 # sum += $t9 addi $s0, $s0, 4 # p++ j L1 L2: add $v0, $zero, $zero # return value from main = 0 jr $ra

Screenshots of code

image text in transcribed

image text in transcribed

image text in transcribed

Exercise C: Translating C code that has a main func- tion Read This First: Start-up and clean-up code When you program in C, typically you think of a function called main as the starting point for your program, and that when your program runs, the last thing it does is return from main. However, in fact, when you build an executable file from C code, that executable will probably contain not only instructions for all of the functions you wrote, but also some instructions that run before main starts and some more instructions that run after main is finished. These extra instructions sometimes go by the name of "start-up and clean-up code. The start-up code is located at the very beginning of the text segment for the program. Starting with this exercise you will write programs by copying a file containing start-up and clean-up code, and also definitions for a main function and perhaps some other functions. To get a program to work, you will never need to modify the start-up and clean-up code. Instead you should edit main, perhaps some static data directives, and perhaps some other functions. What to Do Part 1 Make a directory to work in. Into that directory, download the file stubi.asm. Start up MARS and open the file in the editor. Read the file. Do not worry about the details of the start-up and clean-up code. However, do pay attention to the use of data and text directives, as explained in Figure 5. Assemble and run the program, and note that the clean-up code prints a message including the return value generated by main. Part 2 Now download and take a look at the files add-ints2.c and add-ints2C.asm. The assembly language file contains start-up and clean-up code copied from stubi.asm, and translations of the code in the file. Note the use of the .word directive to allocate and initialize the variables foo, bar, and quux. In MARS, open add-ints2C.asm in the editor and assemble it. In the Execute tab, make sure the Labels window is visible, using the Settings menu. In the Labels window, double-click on foo-that will show you that foo has an initial value of 0x15, and that its neighbour bar has an initial value of 0x2d. In the Text Segment window, scroll down to address Ox00400060, where the first instruction of main is located. For that instruction, check the box in the Bkpt (breakpoint) columnthat will cause the program to pause when it gets to that instruction. Click the Run icon. Notice that the instruction at address Ox00400060 is highlighted, and also that the value of the PC (shown in the Registers tab) is Ox00400060. Now click the Single-Step icon. Note the updates to the PC and to $to. Click the Single-Step icon again and again; after each click note changes to the PC and t-registers. (If you think you have missed something, click on the Undo icon to back up.) Keep doing this until you have seen the value of quuxat address Ox10010034 in the data segment-get updated. Once you have seen that, click the Run icon to let the program run to completion. Part 3 Download the file array-sum2c.asm, load it into the MARS editor and carefully read the last 30 lines or so (starting with the comment # Global variables). Assemble and run the program. Here are some things to check: look for the array elements in the Data Segments window; make sure the values in the local variable registers $30-$s2 are what you expect when the program is finished. Modify the code in array-sum2C.asm so that in addition to putting the sum of the array elements in the local variable sum, it also updates the variable max with the maximum value from the array. Get the logic right-don't just write something that puts a value of - 4 into the $s3 register! To create an if statement you will need a branch instructionthe only two kinds of branch instructions you are allowed to use are beq and bne; you will probably need to use an slt instruction as well. Document each instruction you add with a C-like comment. # add-ints2c.asm # Start-up and clean-up code copied from stubi.asm # 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 .text # adjust $sp, then call main addi Sto, Szero, -32 and $sp,$sp,$to jal main nop # $t0 = Oxffffffeo # round $sp down to multiple of 32 # when main is done, print its return value, then halt the program SW $v0, main_rv la $ao, exit_msg_1 addi $v0, $zero, 4 syscall nop lw $ab, main_rv addi $v0, $zero, 1 syscall nop la $ao, exit_msg_2 addi $v0, $zero, 4 syscall nop addi $v0, Szero, 10 syscall nop # END of start-up & clean-up code. # Global variables. .data .globi foo foo: .word Ox15 globi bar bar: .word ex2d globi quux quux : .word 0 . # Instructions for main .text .globi main main: la Sto, foo lw $t1, ($t0) la St2, bar lw $t3, ($t2) add St4, $t1,$t3 la $t, quux SW $t4, ($t5) add $v0, $zero, $zero jr Sra # $t0 = &foo # $t1 = foo # $t2 = &bar # $t3 = bar # $t4 = $t1 + $t3 # $t5 = &quux # quux = $t4 # return value from main = 0 # 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 # array-sum2c.asm # Start-up and clean-up code copied from stubi.asm # BEGINNING of start-up & clean-up code. Do NOT edit this code. .data exit_mg_1: asciiz "***About to exit. main returned" exit_mg_2: asciiz "." " main_rv: .word .text # adjust $sp, then call main add 1 ste, Szero, -32 and $sp,$sp, ste jal main nop # ste = exffffffee #round $sp down to multiple of 32 # when main is done, print its return value, then halt the program SW Sve, main_rv la Sae, exit_mg_1 addi Sve, Szero, 4 syscall nop lw $ae, main_rv addi $ve, Szero, 1 syscall nop la Sae, exit_mg_2 addi $ve, Szero, 4 Syscall nop addi $ve, Szero, le syscall nop # END of start-up & clean-up code. # Global variables .data #int abc [ ] = {-32, -3, -4, -16, -128, -64} .globl abc abc: word -32, -3, -4, -16, -128, -64 # Hint for checking that the original program works: # The sum of the six array elements is -252, which will be represented # as exffffff04 in a MIPS GPR. # Hint for checking that your final version of the program works: # The maximum of the four array elements is -4, which will be represented # as exfffffffc in a MIPS GPR. # int main(void) # # local variable int op # int *end int sum # int max register $se Ss1 $52 $S3 (to be used when students enhance the program) .text globl main main: la addi add se, att $51, $se, 24 $s2, Szero, Szero L1: #p=abc # end = P + 6 #sum = # if (p - end) goto L2 # $t9 =p #sum + $t9 beg lw add addi j $se, $si, L2 St9, (sse) $s2, $s2, st9 $se, $se, 4 L1 L2: add jr Sve, Szero, Szero Sra # return value from main = @

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago