Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # ELECTRAN QUESTION 9.
- #
- # The first unsigned halfword gives the number of data values to add together.
- # Then the data values follow this given as unsigned 16-bit values.
- # The final memory location allocated with a 'space 2' directive gives the
- # memory location to put the result.
- #
- # No overflow exceptions should result (similar to C-code) when doing the
- # addition - any unsigned overflow should just be ignnored.
- # Your key task is to write some MIPS assembly language to add together the
- # data values given and put the answer into the last memory location.
- #
- # You should write your assembly language below the comment line saying:
- # "WRITE ASSEMBLY LANGUAGE SOLUTION BELOW".
- .data
- vals:
- # Number of data values to add together as a unsigned halfword.
- .half 5
- # The actual data values themselves as "16-bit unsigned values".
- .half 0x0010, 0x0020, 0x0030 ,0x0040, 0x0050
- # Memory address to put the sum into. The result should be stored as a unsigned halfword.
- # In this case the assembly language should put 0x00F0 onto this memory location.
- # However the code should work in general for any given lists of data values.
- .space 2
- ########## WRITE ASSEMBLY LANGUAGE SOLUTION BELOW ##########
- .text
- .globl main
- main:
- la $4, vals # Load address of vals to register $4
- add $10, $0, $0 # Set initial sum to zero
- lhu $5, 0($4) # Load "number of numbers" to $5
- # $s0 = i, $s1 = sum
- addi $17, $0, 0
- add $16, $0, $0
- li $8, 0 # Counter is $8
- for:
- beq $8, $5, done
- add $8, $8, 1 #Move to next for loop iteration
- add $4, $4, 2
- lhu $11, 0($4) #Go to first memloc and get number
- addu $10, $10, $11 # With addu overflow is ignored -> "any unsigned overflow should just be ignnored." -> Adds to previous sum of numbers.
- j for
- done:
- addi $6,$4,2 #Add two to last memory location
- sh $10, 0($6) #Store result to that last memory location.
- li $v0 10 # Exit program using system call 10.
- syscall # syscall 10 (exit)
- #li $5, 2
- #mult $5, $4
- #mflo $12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement