Facebook
From Justin Pau, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 200
  1. .globl read_matrix
  2.  
  3. .text
  4. # ==============================================================================
  5. # FUNCTION: Allocates memory and reads in a binary file as a matrix of integers
  6. #
  7. # FILE FORMAT:
  8. #   The first 8 bytes are two 4 byte ints representing the # of rows and columns
  9. #   in the matrix. Every 4 bytes afterwards is an element of the matrix in
  10. #   row-major order.
  11. # Arguments:
  12. #   a0 (char*) is the pointer to string representing the filename
  13. #   a1 (int*)  is a pointer to an integer, we will set it to the number of rows
  14. #   a2 (int*)  is a pointer to an integer, we will set it to the number of columns
  15. # Returns:
  16. #   a0 (int*)  is the pointer to the matrix in memory
  17. # Exceptions:
  18. # - If malloc returns an error,
  19. #   this function terminates the program with error code 88.
  20. # - If you receive an fopen error or eof,
  21. #   this function terminates the program with error code 90.
  22. # - If you receive an fread error or eof,
  23. #   this function terminates the program with error code 91.
  24. # - If you receive an fclose error or eof,
  25. #   this function terminates the program with error code 92.
  26. # ==============================================================================
  27. read_matrix:
  28.  
  29.         #open the filename
  30.    
  31.     addi sp,sp,-28
  32.     sw ra, 0(sp)
  33.     sw s0, 4(sp)
  34.     sw s1, 8(sp)
  35.     sw s2, 12(sp)
  36.     sw s3, 16(sp)
  37.     sw s4, 20(sp)
  38.     sw s5, 24(sp)
  39.    
  40.    
  41.     ##Initialize variables a0,a1,a2
  42.     mv s0,a0
  43.     mv s1,a1
  44.         mv s2,a2
  45.     mv s3,x0 #bytes to store
  46.     mv s4,x0 #file descriptor
  47.     mv s5,x0
  48.    
  49.    
  50. open_file:  
  51.    
  52.     #Give read instructions for fopen
  53.    
  54.     mv a2,x0 #read permission to 0
  55.     mv a1,s0 #filename of the file we are opening
  56.    
  57.     #Opens the file
  58.     jal ra, fopen
  59.    
  60.     #if a0 is -1
  61.     mv t0,x0
  62.     li t1,1
  63.     sub t0,t0,t1
  64.     beq a0,t0,error
  65.    
  66.    
  67.    
  68.     #a0 is now the file descriptor, if -1 account for an error
  69.     mv s4,a0 #set file descriptor
  70.    
  71. read_file:    
  72.    
  73.     #this is for fread
  74.    
  75.    
  76.     #Prepare malloc
  77.    
  78.    
  79.     #Number of bytes to read 4*(2+m*n)
  80.     li t0,1
  81.     lw t1, 0(s1) #rows
  82.     lw t2, 0(s2) #columns
  83.    
  84.     mul t0,t1,t2
  85.     addi t0,t0,2
  86.     li t3,4
  87.     mul t0,t0,t3
  88.     mv s3, t0
  89.         mv a0, s3 #size of buffer
  90.    
  91.     jal ra, malloc
  92.    
  93.    
  94.     mv a1, s4 #file descriptor
  95.    
  96.     mv a2, a0 #pointer to buffer that we previously malloced
  97.    
  98.     mv a3,s3 #number of bytes to read
  99.    
  100.     jal ra, fread #call fread function
  101.    
  102.    
  103.     #set return value to the address of a2
  104.  
  105.    
  106.     mv s5,a2
  107.     mv a0,s5
  108.  
  109.  
  110.     lw ra, 0(sp)
  111.     lw s0, 4(sp)
  112.     lw s1, 8(sp)
  113.     lw s2, 12(sp)
  114.     lw s3, 16(sp)
  115.     lw s4, 20(sp)
  116.     lw s5, 24(sp)
  117.     addi sp,sp,28
  118.    
  119.    
  120.    #update stack poitner
  121.    
  122.         ret
  123.    
  124. error:
  125.         li a1, 79
  126.         jal exit2
  127.  
  128.  
  129.