# count1s - Count's number of 1's .data .data start: .asciiz "Start counting.\n" done: .asciiz "I'm done.\n" .text #Code goes in text segment .globl main ################################################## main: sw $ra,0($sp) #Push return address addiu $sp,$sp,-4 # onto stack # la $a0,start #Point to start message jal wr_str #Output start message # Start counting and $a0,$0,$0 #Clear $a0 loop: jal wr_hex #Output count in hex jal space #Output a space and $t0,$a0,$a0 #Save $a0 jal count1s #Put number of 1's in $a1 and $a0,$a1,$a1 #Move count to $a0 jal wr_int #Output count jal crlf #Go to new line and $a0,$t0,$t0 #Restore $a0 jal jcnt #Johnson count increment $a0 bne $a0,$0,loop #Loop till $a0 is again 0 # exit: la $a0,done #Use pseudoinstruction jal wr_str #Output done message # addiu $sp,$sp,4 #Pop return address lw $ra,0($sp) # from stack jr $ra #Return ########################################################### # FUNCTION: jcnt - Implements a Johnson counter. When called # the count in $a0 to the next value as shown below. # ... # 00000000H # 80000000H # C0000000H # E0000000H # F0000000H # F8000000H # ... # FFFFFFFFH # 7FFFFFFFH # ... # 00000003H # 00000001H # 00000000H # 80000000H # ... # INPUTS: $a0 - # OUTPUTS: $a0 - # DESTROYS: $a0, $t0 ########################################################### # Do not use pseudoinstructions, and do not assume particular # values in any register. Change only $a0 and $t0. # Comment your code! jcnt: # Enter your code here. jr $ra #Return ############################################################### # FUNCTION: count1s - Returns in $a1 the count of the number # of 1's in $a0 # INPUT: $a0 - Input register # OUTPUT: $a1 - Output of 1's in $a0 # CALLS: NONE # DESTROYS: $a0,$a1 ############################################################### # Do not use pseudoinstructions, and do not assume particular # values in any register. Change only $a0 and $a1. # Comment your code! count1s: # Enter your code here. jr $ra ############################################################### # FUNCTION: wr_hex - Otput integer in Hex to console # INPUT: $a0 - word to be output as hex # OUTPUT: NONE # CALLS: SYSCALL # DESTROYS: $v0 ############################################################### wr_hex: sw $ra,0($sp) #Push return address sw $a0,-4($sp) #Push $a0 onto stack sw $t0,-8($sp) #Push $t0 onto stack sw $t1,-12($sp)#Push $t1 onto stack addiu $sp,$sp,-16 #Fix sp addu $t1,$a0,$0 # Put word in $t1 addiu $t0,$0,32 # Put count in $t0 hexlp: addiu $t0,$t0,-4 # Adjust shift count for next digit srlv $a0,$t1,$t0 # Put digit in ls nibble andi $a0,$a0,0xf # Save only digit addi $a0,$a0,'0' # Convert to ascii addiu $t3,$a0,-0x39# Check for greater than '9' blez $t3,hexskp # if not result OK addiu $a0,$a0,7 # fix it hexskp: jal wr_chr # Output char bgtz $t0,hexlp # Loop till done lw $t1,4($sp) # Pop $t1 lw $t0,8($sp) # Pop $t0 lw $a0,12($sp) #Get $a0 from stack lw $ra,16($sp) #Get return address from stack addiu $sp,$sp,16 #Fix stack pointer jr $ra #Return ############################################################### # # FUNCTION: wr_str - OUTPUT 0 TERMINATED STRING TO CONSOLE # INPUT: $A0 - POINTER TO STRING # OUTPUT: NONE # CALLS: SYSCALL # DESTROYS: $V0 # ############################################################### wr_str: or $a0,$a0,$a0 #nop addi $v0,$0,4 #PRINT STRING REQUEST CODE syscall jr $ra #Return ############################################################### # FUNCTION: crlf - OUTPUT 0 TERMINATED STRING TO CONSOLE # INPUT: NONE # OUTPUT: NONE # CALLS: wr_str # DESTROYS: $v0 ############################################################### .data crlfz: .ascii "\n" .text crlf: sw $ra,0($sp) #Push return address sw $a0,-4($sp) #Push $a0 addiu $sp,$sp,-8 # onto stack la $a0,crlfz #Point to new line string jal wr_str #Output CRLF to console lw $a0,4($sp) #Get $a0 from stack lw $ra,8($sp) #Get return address from stack addiu $sp,$sp,8 #Fix stack jr $ra #Return ############################################################### # # FUNCTION: rd_int - READ INTEGER FROM KEYBOARD # INPUT: NONE # OUTPUT: $a0 - integer read from console # CALLS: SYSCALL # DESTROYS: $v0 # ############################################################### rd_int: addi $v0,$0,5 #Read integer request code syscall add $a0,$v0,$0 #Move integer into $a0 jr $ra #Return ############################################################### # FUNCTION: wr_int - Otput integer to console # INPUT: $a0 - integer # OUTPUT: NONE # CALLS: SYSCALL # DESTROYS: $v0 ############################################################### wr_int: addi $v0,$0,1 #Print integer request code syscall jr $ra #Return ############################################################### # FUNCTION: rd_chr - READ Character from KEYBOARD # INPUT: NONE # OUTPUT: $a0 - Character read from console # CALLS: SYSCALL # DESTROYS: $v0 ############################################################### rd_chr: addi $v0,$0,12 #Read char request code syscall add $a0,$v0,$0 #Move character in $v0 to $a0 jr $ra #Return ############################################################### # FUNCTION: WR_CHR - Output character to console # INPUT: $a0 - Character to output to console # OUTPUT: NONE # CALLS: SYSCALL # DESTROYS: $v0 ############################################################### wr_chr: addi $v0,$0,11 #Print character request code syscall jr $ra #Return ############################################################### # FUNCTION: space - Output a space to console # INPUT: NONE # OUTPUT: NONE # CALLS: SYSCALL # DESTROYS: $v0 ############################################################### space: sw $a0,0($sp) #Push $a0 addiu $sp,$sp,-4 # onto stack addi $a0,$0,' ' #Set character to a space addi $v0,$0,11 #Print character request code syscall addiu $sp,$sp,4 #Pop $a0 lw $a0,0($sp) # from stack jr $ra #Return ########################################################### # FUNCTION: UPPER - IF ASCII character (see Fig 3.15) in $a0 # is between 'a' and 'z' convert to upper case # else $a0 is unchanged. # Note: A lower case ASCII character can be # converted to upeercase by subtracting 32. # INPUTS: $a0 - Contains 8 bit ASCII character in least # significant byte. High order bytes are 0. # OUTPUTS: $a0 - Contains input ASCII character converted # to upper case. # DESTROYS: $a0, $s0 ########################################################### UPPER: slti $s0,$a0,'a' #Compare input char with 'a' bgtz $s0,Done #Exit if less than 'a' slti $s0,$a0,'z'+1 #Compare input char with 'z' blez $s0,Done #Exit if greater than 'z' addi $a0,-32 #Convert char to upper case. Done: jr $ra #Return .data