Advertisement
Nickanick12

Untitled

Sep 1st, 2023
1,467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.88 KB | None | 0 0
  1. # Part 1: Create space in the data section for the header information.
  2. .data
  3. bmp_header: .space 54  # Allocate 54 bytes for BMP header
  4. bmp_filename: .asciiz "pillarscipher.bmp"
  5. not_found_text: .asciiz "Asterisks not found"
  6.  
  7. .text
  8. # Open the file for reading (syscall 13)
  9. li $v0, 13             # syscall 13 for opening a file
  10. la $a0, bmp_header # Load the file name
  11. li $a1, 0              # Open in read-only mode
  12. li $a2, 0              # File permission (ignored)
  13. syscall
  14.  
  15. # Read the first two bytes of the BMP file (syscall 14)
  16. li $v0, 14             # syscall 14 for reading a file
  17. la $a0, bmp_header     # Load address of bmp_header to read into
  18. li $a1, 2              # Read 2 bytes
  19. li $a2, 0              # File descriptor (from the previous syscall)
  20. syscall
  21.  
  22. # Access the BMP header data and answer the questions
  23. # 1) What are the first two bytes of the BMP file?
  24. lb $t0, bmp_header     # Load the first byte into $t0
  25. lb $t1, bmp_header+1   # Load the second byte into $t1
  26.  
  27. # Calculate the file size (assuming it's little-endian)
  28. lw $t2, bmp_header+2   # Load the file size into $t2
  29.  
  30. # Extract the Bits Per Pixel value
  31. lw $t3, bmp_header+28  # Load Bits Per Pixel into $t3
  32.  
  33. # Part 2: Allocate space on heap using the size information (syscall 9)
  34. li $v0, 9              # syscall 9 for sbrk (allocate heap space)
  35. move $a0, $t2          # Size of the file
  36. li $v1, 4              # Allocate in units of 4 bytes (words)
  37. syscall
  38. # Now, you have heap space for the file content in $v0
  39.  
  40. # Reopen the file to read the rest of the data
  41. li $v0, 13             # syscall 13 for opening a file
  42. la $a0, bmp_header # Load the file name again
  43. li $a1, 0              # Open in read-only mode
  44. li $a2, 0              # File permission (ignored)
  45. syscall
  46.  
  47. # Read the file content and store it in the heap space
  48. li $v0, 14             # syscall 14 for reading a file
  49. la $a0, 0($v0)         # Address of the heap space
  50. move $a1, $t2          # Number of bytes to read (file size)
  51. li $a2, 0              # File descriptor (from the previous syscall)
  52. syscall
  53.  
  54. # Part 3: Search through the heap to find the clue (city following three asterisks)
  55. la $t4, 0($v0)         # Load address of the heap space
  56. li $t5, 0x2A2A2A       # ASCII value for three asterisks: '***'
  57. search_loop:
  58.    lb $t6, ($t4)      # Load a byte from the heap
  59.    beqz $t6, not_found  # If it's null, the asterisks were not found
  60.     beq $t6, $t5, found_clue  # If three asterisks are found, jump to found_clue
  61.     addi $t4, $t4, 1   # Move to the next byte
  62.     j search_loop
  63.  
  64. not_found:
  65.     # Print a message indicating that the asterisks were not found
  66.     li $v0, 4
  67.     la $a0, not_found_text
  68.     syscall
  69.  
  70. found_clue:
  71.     # Define a buffer to store the city name
  72.     .data
  73.     city_buffer: .space 100  # Adjust the size as needed
  74.  
  75.     # Initialize indices and counters
  76.     li $t7, 0     # Index for city_buffer
  77.     li $t8, 0     # Counter for the number of characters processed
  78.  
  79.     copy_loop:
  80.         lb $t9, ($t4)      # Load a byte from the heap
  81.         sb $t9, city_buffer($t7)  # Store it in city_buffer
  82.         beqz $t9, end_copy  # If it's null (end of city name), exit the loop
  83.  
  84.        # Move to the next byte in both source and destination
  85.        addi $t4, $t4, 1
  86.        addi $t7, $t7, 1
  87.  
  88.        # Check for the maximum city name length (e.g., 99 characters)
  89.        # If you have a specific maximum length, you can adjust this value
  90.        li $t6, 99  # Maximum city name length
  91.        bge $t8, $t6, end_copy
  92.  
  93.        # Increment the counter
  94.        addi $t8, $t8, 1
  95.        j copy_loop
  96.  
  97.    end_copy:
  98.        # Null-terminate the extracted city name
  99.        sb $zero, city_buffer($t7)
  100.  
  101.        # Display the extracted city name (null-terminated string)
  102.        li $v0, 4
  103.        la $a0, city_buffer
  104.        syscall
  105.  
  106.    # Exit the program
  107.    li $v0, 10             # syscall 10 to exit
  108.    syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement