Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Print representation of a range of bits in a word.
- # Word w in register $a0 and number of bits n in register $a1
- # Print out bits n-1 to 0 of w in a row, using '#' for a 1 bit and '.' for a zero bit
- # Data
- .data
- hash: .asciiz "#"
- period: .asciiz "."
- new_line: .asciiz "\n"
- input_prompt: .asciiz "input: "
- char_pats: .word 0x69996, 0x26227, 0x6924f, 0xe161e, 0x26af2, 0xf8e1e, 0x78e96, 0xf1248, 0x69696, 0x6971e
- input_buffer: .space 13
- .globl main
- .text
- # Main Program
- main:
- # Store constant values
- addi $s0, $zero, 4 # Num Cols
- addi $s1, $zero, 5 # Num Rows
- addi $s2, $zero, 15 # Number of characters for input buffer to accept
- bne $a0, 0, test_code
- # Prompt user for input
- li $v0, 4
- la $a0, input_prompt
- syscall
- # Read input from user
- li $v0, 8
- la $a0, input_buffer
- li $a1, 13
- syscall
- # Print new line
- li $v0, 4
- la $a0, new_line
- syscall
- # Now loop through the rows storing the current row number in $s3
- addi $s3, $zero, 0
- row_loop:
- # Loop through all characters in input string, store current character position in $s4
- addi $s4, $zero, 0
- character_loop:
- # Get current character and store in $s5
- la $s5, input_buffer
- add $s5, $s5, $s4
- lb $s6, ($s5)
- # If current character is the new line or the null byte, end the row
- beq $s6, '\n', end_character_loop
- beq $s6, '\0', end_character_loop
- # Get row pattern
- add $a0, $zero, $s6
- add $a1, $zero, $s3
- jal get_row_pattern
- # Print bits
- addi $s7, $s0, 2 # Add 2 to the number of bits to print to space out characters
- add $a0, $zero, $v0
- add $a1, $zero, $s7
- jal print_bits
- addi $s4, $s4, 1
- bne $s4, $s2, character_loop
- end_character_loop:
- # Print new line
- li $v0, 4
- la $a0, new_line
- syscall
- addi $s3, $s3, 1
- bne $s3, $s1, row_loop
- #addi $a0, $zero, '4'
- #addi $a1, $zero, 4
- #jal get_row_pattern
- jal end
- test_code:
- # Print first row of '4'
- li $a0, 0x34
- li $a1, 0
- jal get_row_pattern
- move $a0, $v0
- li $a1, 6
- jal print_bits
- # Print new line
- li $v0, 11
- li $a0, 0x0a
- syscall
- # Print second row of '4'
- li $a0, 0x34
- li $a1, 1
- jal get_row_pattern
- move $a0, $v0
- li $a1, 6
- jal print_bits
- # Print new line
- li $v0, 11
- li $a0, 0x0a
- syscall
- # End program
- jal end
- # Functions
- # Character stored in $a0
- # Row number stored in $a1
- get_row_pattern:
- # Check supplied character is in range
- blt $a0, '0', else
- bgt $a0, '9', else
- # Load pattern from memory and store in $t3
- addi $t0, $a0, -48 # Get pattern index and store in $t0
- add $t0, $t0, $t0 # Double the index
- add $t0, $t0, $t0 # ^^^
- la $t1, char_pats # Store address of char_pats in $t1
- add $t2, $t0, $t1 # Add both address components together
- lw $t3, 0($t2) # Get entry from array
- # Calculate bitmask offset and store in $t4
- mult $s0, $s1
- mflo $t4
- sub $t4, $t4, $s0
- mult $a1, $s0
- mflo $t5 # $t5 can be overrwritten after next instruction
- sub $t4, $t4, $t5
- # Calculate bitmask by doing left shift with the offset calculated above and store in $t5
- addi $t5, $zero, 0xf
- sllv $t5, $t5, $t4
- # Do bitwise AND using the bitmask and the pattern and store the result in $t6
- and $t6, $t3, $t5
- # Shift $t6 to the right using the same offset as calculated above
- srlv $t6, $t6, $t4
- # Store the pattern in $v0
- add $v0, $zero, $t6
- jr $ra
- else:
- addi $v0, $zero, 0xf
- jr $ra
- print_bits:
- # Store value for (n-1) in $t0
- addi $t0, $a1, -1
- # Create bit mask and store in register $t1
- addi $t1, $zero, 1 # Store value of 1 in the bit mask register
- sllv $t1, $t1, $t0 # Do bitwise shift
- # Move value of $a0 into $t3
- add $t3, $a0, $zero
- # Loop
- loop:
- # Do bitwise AND between w and the mask then store result in register $t2
- and $t2, $t3, $t1
- bne $t2, $zero, print_hash
- # Print period
- li $v0, 4
- la $a0, period
- syscall
- beq $t2, $zero, end_if
- print_hash:
- # Print hash
- li $v0, 4
- la $a0, hash
- syscall
- end_if:
- srl $t1, $t1, 1 # Right bitwise shift mask by 1
- bne $t1, $zero, loop # Loop again if mask != 0
- jr $ra
- end:
- # End program
- li $v0, 10
- syscall
Advertisement
Add Comment
Please, Sign In to add comment