Guest User

Untitled

a guest
Oct 29th, 2013
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Print representation of a range of bits in a word.
  2. # Word w in register $a0 and number of bits n in register $a1
  3. # Print out bits n-1 to 0 of w in a row, using '#' for a 1 bit and '.' for a zero bit
  4.  
  5. # Data
  6. .data
  7. hash: .asciiz "#"
  8. period: .asciiz "."
  9. new_line: .asciiz "\n"
  10. input_prompt: .asciiz "input: "
  11. char_pats: .word 0x69996, 0x26227, 0x6924f, 0xe161e, 0x26af2, 0xf8e1e, 0x78e96, 0xf1248, 0x69696, 0x6971e
  12. input_buffer: .space 13
  13.  
  14. .globl main
  15.  
  16. .text
  17.  
  18. # Main Program
  19. main:
  20.     # Store constant values
  21.     addi $s0, $zero, 4 # Num Cols
  22.     addi $s1, $zero, 5 # Num Rows
  23.     addi $s2, $zero, 15 # Number of characters for input buffer to accept
  24.    
  25.     bne $a0, 0, test_code
  26.    
  27.     # Prompt user for input
  28.     li $v0, 4
  29.     la $a0, input_prompt
  30.     syscall
  31.    
  32.     # Read input from user
  33.     li $v0, 8
  34.     la $a0, input_buffer
  35.     li $a1, 13
  36.     syscall
  37.    
  38.     # Print new line
  39.     li $v0, 4
  40.     la $a0, new_line
  41.     syscall
  42.    
  43.     # Now loop through the rows storing the current row number in $s3
  44.     addi $s3, $zero, 0
  45.     row_loop:
  46.         # Loop through all characters in input string, store current character position in $s4
  47.         addi $s4, $zero, 0
  48.         character_loop:
  49.             # Get current character and store in $s5
  50.             la $s5, input_buffer
  51.             add $s5, $s5, $s4
  52.             lb $s6, ($s5)
  53.            
  54.             # If current character is the new line or the null byte, end the row
  55.             beq $s6, '\n', end_character_loop
  56.             beq $s6, '\0', end_character_loop
  57.        
  58.             # Get row pattern
  59.             add $a0, $zero, $s6
  60.             add $a1, $zero, $s3
  61.             jal get_row_pattern
  62.            
  63.             # Print bits
  64.             addi $s7, $s0, 2 # Add 2 to the number of bits to print to space out characters
  65.             add $a0, $zero, $v0
  66.             add $a1, $zero, $s7
  67.             jal print_bits
  68.            
  69.             addi $s4, $s4, 1
  70.         bne $s4, $s2, character_loop
  71.         end_character_loop:
  72.         # Print new line
  73.         li $v0, 4
  74.         la $a0, new_line
  75.         syscall
  76.         addi $s3, $s3, 1
  77.     bne $s3, $s1, row_loop
  78.    
  79.  
  80.     #addi $a0, $zero, '4'
  81.     #addi $a1, $zero, 4
  82.     #jal get_row_pattern
  83.  
  84.     jal end
  85.    
  86. test_code:
  87.     # Print first row of '4'
  88.     li $a0, 0x34
  89.     li $a1, 0
  90.     jal get_row_pattern
  91.     move $a0, $v0
  92.     li $a1, 6
  93.     jal print_bits
  94.    
  95.     # Print new line
  96.     li $v0, 11
  97.     li $a0, 0x0a
  98.     syscall
  99.    
  100.     # Print second row of '4'
  101.     li $a0, 0x34
  102.     li $a1, 1
  103.     jal get_row_pattern
  104.     move $a0, $v0
  105.     li $a1, 6
  106.     jal print_bits
  107.    
  108.     # Print new line
  109.     li $v0, 11
  110.     li $a0, 0x0a
  111.     syscall
  112.    
  113.     # End program
  114.     jal end
  115.  
  116. # Functions
  117.  
  118. # Character stored in $a0
  119. # Row number stored in $a1
  120. get_row_pattern:
  121.     # Check supplied character is in range
  122.     blt $a0, '0', else
  123.     bgt $a0, '9', else
  124.         # Load pattern from memory and store in $t3
  125.         addi $t0, $a0, -48 # Get pattern index and store in $t0
  126.         add $t0, $t0, $t0 # Double the index
  127.         add $t0, $t0, $t0 # ^^^
  128.         la $t1, char_pats # Store address of char_pats in $t1
  129.         add $t2, $t0, $t1 # Add both address components together
  130.         lw $t3, 0($t2) # Get entry from array
  131.        
  132.         # Calculate bitmask offset and store in $t4
  133.         mult $s0, $s1
  134.         mflo $t4
  135.         sub $t4, $t4, $s0
  136.         mult $a1, $s0
  137.         mflo $t5 # $t5 can be overrwritten after next instruction
  138.         sub $t4, $t4, $t5
  139.        
  140.         # Calculate bitmask by doing left shift with the offset calculated above and store in $t5
  141.         addi $t5, $zero, 0xf
  142.         sllv $t5, $t5, $t4
  143.        
  144.         # Do bitwise AND using the bitmask and the pattern and store the result in $t6
  145.         and $t6, $t3, $t5
  146.        
  147.         # Shift $t6 to the right using the same offset as calculated above
  148.         srlv $t6, $t6, $t4
  149.        
  150.         # Store the pattern in $v0
  151.         add $v0, $zero, $t6
  152.         jr $ra
  153.     else:
  154.         addi $v0, $zero, 0xf
  155.         jr $ra
  156.  
  157. print_bits:
  158.     # Store value for (n-1) in $t0
  159.     addi $t0, $a1, -1
  160.     # Create bit mask and store in register $t1
  161.     addi $t1, $zero, 1 # Store value of 1 in the bit mask register
  162.     sllv $t1, $t1, $t0 # Do bitwise shift
  163.    
  164.     # Move value of $a0 into $t3
  165.     add $t3, $a0, $zero
  166.    
  167.     # Loop
  168.     loop:
  169.         # Do bitwise AND between w and the mask then store result in register $t2
  170.         and $t2, $t3, $t1
  171.         bne $t2, $zero, print_hash
  172.             # Print period
  173.             li $v0, 4
  174.             la $a0, period
  175.             syscall
  176.         beq $t2, $zero, end_if
  177.         print_hash:
  178.             # Print hash
  179.             li $v0, 4
  180.             la $a0, hash
  181.             syscall
  182.         end_if:
  183.         srl $t1, $t1, 1 # Right bitwise shift mask by 1
  184.     bne $t1, $zero, loop # Loop again if mask != 0
  185.    
  186.     jr $ra
  187.  
  188. end:
  189.     # End program
  190.     li $v0, 10
  191.     syscall
Advertisement
Add Comment
Please, Sign In to add comment