# This program does a histogram on the character string str. # This file was written in the Context editor with smart tabs # turned off (Options -> Environment -> Editor -> Smart Tabs). .data # The following entries go in the data segment strSpace: .asciiz " " strNewline: .asciiz "\n" str: .asciiz "AACCEEGGIIKKMMOOQQSSUUWWYYabcdefghijklmnopqrstuvwxyz" .align 2 # ensure table is word aligned histo: .space 1024 # 256 words .text # The following entries go in the text (program code) segment main: li $v0,4 # load print_string code la $a0,str # load address of the string to print syscall # ask the system to print the string li $v0,4 # load print_string code la $a0,strNewline # load address of the string to print syscall # ask the system to print the string # zero out the histogram block la $t0,histo # start address addi $t1,$t0,1024 # end address initLoop: sw $zero,0($t0) # store zero addi $t0,$t0,4 # increment address pointer blt $t0,$t1,initLoop # loop if more to do # implement problem 7 code here move $t0,$zero # $t0 = character index = 0 la $t1,str # $t1 = address(str) la $t2,histo # $t2 = address(histo) histoLoop: add $t3,$t1,$t0 # address of next character lbu $t4,0($t3) # $t4 = next character sll $t4,$t4,2 # convert to word offset add $t5,$t2,$t4 # address of histo entry lw $t6,0($t5) # load the histogram entry addi $t6,1 # increment it sw $t6,0($t5) # store it back in memory addi $t0,$t0,1 # increment the character index bnez $t4,histoLoop # loop if not at end # print the contents of the histogram la $t0,histo # $t0 = address of the first word of the histo array addi $t1,$t0,1024 # $t1 = address of word following the histo array move $t2,$zero # $t2 = index = 0 printLoop: li $v0,1 # load print_integer code move $a0,$t2 # put the index in $a0 for printing syscall # ask the system to print the integer li $v0,4 # load print_string code la $a0,strSpace # load address of the string to print syscall # ask the system to print the string li $v0,1 # load print_integer code lw $a0,0($t0) # put the histogram value in $a0 for printing syscall # ask the system to print the integer li $v0,4 # load print_string code la $a0,strNewline # load address of the string to print syscall # ask the system to print the string addi $t0,$t0,4 # $t0 = address(next entry) addi $t2,$t2,1 # $t2 = index(next entry) bne $t0,$t1,printLoop # loop if more to do jr $ra # return