Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # ELECTRAN QUESTION 9.
  3. #
  4. # The first unsigned halfword gives the number of data values to add together.
  5. # Then the data values follow this given as  unsigned 16-bit values.
  6. # The final memory location allocated with a  'space  2' directive gives the
  7. # memory location to put the result.
  8. #
  9. # No overflow exceptions should result (similar to C-code) when doing the
  10. # addition - any  unsigned overflow should just be ignnored.
  11.  
  12. # Your key task is to write some MIPS assembly language to add together the
  13. # data values given and put the answer into the  last memory location.
  14. #
  15. # You should write your assembly language below the comment line saying:
  16. # "WRITE ASSEMBLY LANGUAGE SOLUTION BELOW".
  17.  
  18. .data
  19.  
  20. vals:
  21.  
  22. # Number of data values to add together as a unsigned halfword.
  23. .half 5
  24.  
  25. # The actual data values themselves as "16-bit unsigned values".
  26. .half 0x0010, 0x0020, 0x0030 ,0x0040, 0x0050
  27.  
  28. # Memory address to put the sum into.  The result should be stored as a unsigned halfword.
  29. # In this case the assembly language should put  0x00F0 onto this memory location.
  30. # However the code should work in general for any given lists of data values.
  31. .space 2
  32.  
  33.  
  34. ########## WRITE ASSEMBLY LANGUAGE SOLUTION BELOW ##########
  35.     .text
  36.     .globl main
  37.  
  38. main:
  39.     la  $4, vals      # Load address of vals to register $4
  40.     add $10, $0, $0   # Set initial sum to zero
  41.     lhu $5, 0($4)     # Load "number of numbers" to $5
  42.  
  43.     # $s0 = i, $s1 = sum
  44.    
  45.     addi $17, $0, 0
  46.     add  $16, $0, $0
  47.     li $8, 0    # Counter is $8
  48.        
  49. for:    
  50.     beq $8, $5, done
  51.     add $8, $8, 1 #Move to next for loop iteration
  52.     add $4, $4, 2
  53.     lhu $11, 0($4)    #Go to first memloc and get number
  54.     addu $10, $10, $11 # With addu overflow is ignored -> "any  unsigned overflow should just be ignnored." -> Adds to previous sum of numbers.
  55.        
  56.     j for
  57.  
  58. done:  
  59.     addi $6,$4,2     #Add two to last memory location
  60.     sh  $10, 0($6)   #Store result to that last memory location.
  61.    
  62.     li $v0 10         # Exit program using system call 10.
  63.     syscall           # syscall 10 (exit)
  64.  
  65.     #li  $5, 2
  66.     #mult $5, $4
  67.     #mflo $12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement