Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. .globl read_matrix
  2.  
  3. .text
  4. # ==============================================================================
  5. # FUNCTION: Allocates memory and reads in a binary file as a matrix of integers
  6. # If any file operation fails or doesn't read the proper number of bytes,
  7. # exit the program with exit code 1.
  8. # FILE FORMAT:
  9. # The first 8 bytes are two 4 byte ints representing the # of rows and columns
  10. # in the matrix. Every 4 bytes afterwards is an element of the matrix in
  11. # row-major order.
  12. # Arguments:
  13. # a0 is the pointer to string representing the filename
  14. # a1 is a pointer to an integer, we will set it to the number of rows
  15. # a2 is a pointer to an integer, we will set it to the number of columns
  16. # Returns:
  17. # a0 is the pointer to the matrix in memory
  18. # ==============================================================================
  19. read_matrix:
  20.  
  21. # Prologue
  22. addi sp, sp, -36
  23. sw ra, 0(sp)
  24. sw s0, 4(sp)
  25. sw s1, 8(sp)
  26. sw s2, 12(sp)
  27. sw s3, 16(sp)
  28. sw s4, 20(sp)
  29. sw s5, 24(sp)
  30. sw s6, 28(sp)
  31. sw s7, 32(sp)
  32.  
  33. mv s0, a0 # preserve arguments
  34. mv s1, a1
  35. mv s2, a2
  36.  
  37. li t1, 4 # size of bytes
  38. mv a1, a0 # move file name into a1
  39. li a2, 0 # read permissions
  40. jal ra fopen # open file
  41. addi t0, x0, -1
  42. beq x0 a0 eof_or_error # check error file
  43. mv t0, a0 # t0 is now file descriptor
  44. # read rows
  45. mv a1, t0
  46. mv a2, s1
  47. mv a3, t1 # 4 bytes
  48. mv s6, a3 # save this variable
  49. jal ra fread
  50. bne a0, s6, eof_or_error
  51. lw s3, 0(a2) # load num of rows
  52. # read columns
  53. mv a1, t0
  54. mv a2, s2
  55. mv a3, t1 # 4 bytes
  56. mv s6, a3 # save this variable
  57. jal ra fread
  58. bne a0, s6, eof_or_error
  59. lw s4, 0(a2) # load num of columns
  60. # mutiply and malloc space
  61. mul s5, s3, s4 # number of entries
  62. mul s5, s5, t1 # mul by byte size
  63. mv a0, s5 # malloc s5 amount of memory
  64. jal ra malloc # returns mallocd pointer to matrix in a0
  65.  
  66. # read rest of matrix
  67. mv a1, t0 # file descriptor
  68. mv a2, a0 # mallocd memory
  69. mv a3, s5 # size
  70.  
  71. mv s6, a3 # save byte length
  72. mv s7, a0 # save memory pointer
  73. jal ra fread
  74. bne a0, s6, eof_or_error
  75.  
  76. # close file
  77. mv a1, t0
  78. jal ra fclose
  79. bne a0, x0, eof_or_error
  80. mv a0, s7 # move pointer to a0
  81. mv a1, s3 # num rows
  82. mv a2, s4 # num columns
  83.  
  84. # Epilogue
  85. lw ra, 0(sp)
  86. lw s0, 4(sp)
  87. lw s1, 8(sp)
  88. lw s2, 12(sp)
  89. lw s3, 16(sp)
  90. lw s4, 20(sp)
  91. lw s5, 24(sp)
  92. lw s6, 28(sp)
  93. lw s7, 32(sp)
  94. addi sp, sp, 36
  95. ret
  96.  
  97. eof_or_error:
  98. li a1 1
  99. jal exit2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement