Beginner-level Assembly: DO NOT USE ANY PSEUDO-INSTRUCTIONS -and- COMMENT EVERY
ID: 3904036 • Letter: B
Question
Beginner-level Assembly: DO NOT USE ANY PSEUDO-INSTRUCTIONS -and- COMMENT EVERY LINE OF CODE YOU ADD
The purpose of the program is to read in a list of 10 integers and then print them out. There are 4 functions in the program;
2 of which you are to complete.
Main: This function will call getdata and print. Main is complete and should not be changed.
Getdata: This function reads 10 integers and stores them into memory. There is one parameter ($a0) which holds the address of where the list of numbers is to be stored. Getdata calls the function printstr to print the prompt. Getdata is complete and should not be changed.
Printstr: This function is to print a string. There is one parameter ($a0) which holds the address of the string to print. You are to complete this function so it performs as required. This function is called from the getdata function and the print function.
Print: This function is to print the list of integers. The integers must all be on the same line of output and be separated by a comma and space (already in the data segment). See the example input and output below. This function must call the printstr function to print the comma string. Remember that as this function calls another, it must save and restore the $ra.
-
-
Specifics:
- Do not use pseudo-instructions
- Make sure that your print function calls the printstr function correctly and that the stack is used correctly.
-
-
Documentation:
- Comment every instruction/line of code.
-
-
Sample input and output. Note there is no comma after the last number.
Enter a number 1
Enter a number 2
Enter a number 9
Enter a number 11
Enter a number 99
Enter a number 12
Enter a number 24
Enter a number 90
Enter a number 17
Enter a number 82
1, 2, 9, 11, 99, 12, 24, 90, 7, 82
-
-
-
Starting code is shown below
-
-
-
# make sure to add a comment to every line you add
# do not use any pseudo-instructions
# do not change the Main or Getdata functions
.data
prompt: .asciiz "Enter a number " # address 0x10010000
comma: .asciiz ", " # address 0x10010010
.space 40 # set aside 40 bytes for 10 integers
.globl main
.text
main:
lui $a0, 0x1001 # get start of data segment
jal getdata # call getdata function
lui $a0, 0x1001 # get start of data segment
ori $a0, $a0, 0x0014
jal print # call print function
ori $v0, $0, 10 # set command to exit
syscall # end program
# printstr
# paramters: $a0 holds address of string to print
# purpose: print the string parameter
printstr:
#to be completed
jr $ra
getdata:
addi $sp, $sp, -4 # allocate space on stack
sw $ra, 0($sp) # save $ra
lui $t1, 0x1001 #start of data
ori $t1, $t1, 0x0014 # address of first input integer
ori $t0, $0, 10 # counter
top: beq $t0, $0, ret # while not 0
lui $a0, 0x1001
ori $a0, $a0, 0x0000
jal printstr # call function to print prompt
ori $a0, $a0, 0x0014 # $a0 now holds address of first integer
ori $v0, $0, 5 # set command to read integer
syscall # read int
sw $v0, 0($t1) # save int in memory
addi $t1, $t1, 4 # increment to next location
addi $t0, $t0, -1 # decrement counter
j top # repeat
ret: lw $ra, 0($sp) # restore return address
addi $sp, $sp, 4 # clean up stack
jr $ra # return from call
# print
# parameter: $a0 holds address of list in memory
# purpose: print the list separated by commas
print:
#to be completed
jr $ra
Explanation / Answer
ScreenShot
------------------------------------------------------------------------
Code
#variable declaration
.data
prompt: .asciiz "Enter a number " # address 0x10010000
comma: .asciiz ", " # address 0x10010010
.space 40 # set aside 40 bytes for 10 integers
#main function
.globl main
.text
main:
lui $a0, 0x1001 # get start of data segment
jal getdata # call getdata function
lui $a0, 0x1001 # get start of data segment
ori $a0, $a0, 0x0014
jal print # call print function
ori $v0, $0, 10 # set command to exit
syscall # end program
# printstr
# paramters: $a0 holds address of string to print
# purpose: print the string parameter
printstr:
#system call for string display
addi $v0,$0,4
syscall
jr $ra
#get data into an array function
getdata:
addi $sp, $sp, -4 # allocate space on stack
sw $ra, 0($sp) # save $ra
lui $t1, 0x1001 #start of data
ori $t1, $t1, 0x0014 # address of first input integer
ori $t0, $0, 10 # counter
top: beq $t0, $0, ret # while not 0
lui $a0, 0x1001
ori $a0, $a0, 0x0000
jal printstr # call function to print prompt
ori $a0, $a0, 0x0014 # $a0 now holds address of first integer
ori $v0, $0, 5 # set command to read integer
syscall # read int
sw $v0, 0($t1) # save int in memory
addi $t1, $t1, 4 # increment to next location
addi $t0, $t0, -1 # decrement counter
j top # repeat
ret: lw $ra, 0($sp) # restore return address
addi $sp, $sp, 4 # clean up stack
jr $ra # return from call
# print
# parameter: $a0 holds address of list in memory
# purpose: print the list separated by commas
print:
#move the starting address from a0 to t1
add $t1,$0,$a0
ori $t0, $0, 10 # counter
#loop to print each data comma seperated
loop:
beq $t0, $0, retprint # while not 0
#get first data in a0
lw $a0, 0($t1)
#print system call
addi $v0,$0,1
syscall
beq $t0, $2, retprint # avoid last comma
#comma seperation address
lui $a0, 0x1001 # get start of data segment
ori $a0, $a0, 0x0010
#print comma
addi $v0,$0,4
syscall
addi $t1, $t1, 4 # increment to next location
addi $t0, $t0, -1 # decrement counter
j loop # repeat
#return from print
retprint:
jr $ra
--------------------------------------------------------------------
Output
Enter a number 1
Enter a number 2
Enter a number 9
Enter a number 11
Enter a number 99
Enter a number 12
Enter a number 24
Enter a number 90
Enter a number 7
Enter a number 82
1, 2, 9, 11, 99, 12, 24, 90, 7, 82
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.