Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Part 1: Create space in the data section for the header information.
- .data
- bmp_header: .space 54 # Allocate 54 bytes for BMP header
- bmp_filename: .asciiz "pillarscipher.bmp"
- not_found_text: .asciiz "Asterisks not found"
- .text
- # Open the file for reading (syscall 13)
- li $v0, 13 # syscall 13 for opening a file
- la $a0, bmp_header # Load the file name
- li $a1, 0 # Open in read-only mode
- li $a2, 0 # File permission (ignored)
- syscall
- # Read the first two bytes of the BMP file (syscall 14)
- li $v0, 14 # syscall 14 for reading a file
- la $a0, bmp_header # Load address of bmp_header to read into
- li $a1, 2 # Read 2 bytes
- li $a2, 0 # File descriptor (from the previous syscall)
- syscall
- # Access the BMP header data and answer the questions
- # 1) What are the first two bytes of the BMP file?
- lb $t0, bmp_header # Load the first byte into $t0
- lb $t1, bmp_header+1 # Load the second byte into $t1
- # Calculate the file size (assuming it's little-endian)
- lw $t2, bmp_header+2 # Load the file size into $t2
- # Extract the Bits Per Pixel value
- lw $t3, bmp_header+28 # Load Bits Per Pixel into $t3
- # Part 2: Allocate space on heap using the size information (syscall 9)
- li $v0, 9 # syscall 9 for sbrk (allocate heap space)
- move $a0, $t2 # Size of the file
- li $v1, 4 # Allocate in units of 4 bytes (words)
- syscall
- # Now, you have heap space for the file content in $v0
- # Reopen the file to read the rest of the data
- li $v0, 13 # syscall 13 for opening a file
- la $a0, bmp_header # Load the file name again
- li $a1, 0 # Open in read-only mode
- li $a2, 0 # File permission (ignored)
- syscall
- # Read the file content and store it in the heap space
- li $v0, 14 # syscall 14 for reading a file
- la $a0, 0($v0) # Address of the heap space
- move $a1, $t2 # Number of bytes to read (file size)
- li $a2, 0 # File descriptor (from the previous syscall)
- syscall
- # Part 3: Search through the heap to find the clue (city following three asterisks)
- la $t4, 0($v0) # Load address of the heap space
- li $t5, 0x2A2A2A # ASCII value for three asterisks: '***'
- search_loop:
- lb $t6, ($t4) # Load a byte from the heap
- beqz $t6, not_found # If it's null, the asterisks were not found
- beq $t6, $t5, found_clue # If three asterisks are found, jump to found_clue
- addi $t4, $t4, 1 # Move to the next byte
- j search_loop
- not_found:
- # Print a message indicating that the asterisks were not found
- li $v0, 4
- la $a0, not_found_text
- syscall
- found_clue:
- # Define a buffer to store the city name
- .data
- city_buffer: .space 100 # Adjust the size as needed
- # Initialize indices and counters
- li $t7, 0 # Index for city_buffer
- li $t8, 0 # Counter for the number of characters processed
- copy_loop:
- lb $t9, ($t4) # Load a byte from the heap
- sb $t9, city_buffer($t7) # Store it in city_buffer
- beqz $t9, end_copy # If it's null (end of city name), exit the loop
- # Move to the next byte in both source and destination
- addi $t4, $t4, 1
- addi $t7, $t7, 1
- # Check for the maximum city name length (e.g., 99 characters)
- # If you have a specific maximum length, you can adjust this value
- li $t6, 99 # Maximum city name length
- bge $t8, $t6, end_copy
- # Increment the counter
- addi $t8, $t8, 1
- j copy_loop
- end_copy:
- # Null-terminate the extracted city name
- sb $zero, city_buffer($t7)
- # Display the extracted city name (null-terminated string)
- li $v0, 4
- la $a0, city_buffer
- syscall
- # Exit the program
- li $v0, 10 # syscall 10 to exit
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement