#################################################################### #Program #3 Right Align Misery Due Date: Oct. 17, 2001 # #Programmer: Katrina A. "Kat" Templeton Course: MIS 24 Sec: 0748 # #Last Modified: Oct. 14, 0:36. # #Needs: Tweaks to hex, commentary, and displaying all +/- e/o vals # # better functional description # #################################################################### #Functional description: # #A program to generate a random number from a seed value, and keeps# #a running tally of random numbers (both in decimal and hex). # #################################################################### #Psuedocode Description: # # print greetz; # $t0=6925; # # $t1=3021337; # # $t3=0; # # for($t2=32; $t2 > 0; $t2 = $t2 - 1) { # # $t0 = $t0 * $t1 +5923; # $t5 = and($t0,1) # if ($t5 = 0) { # $t6=$t6+1;} # if ($t0 <= 0) { # # $t7=$t7+1; } # # $a0=$t0; # # jump PrintDecimal (see PrintDecimal for that pseudocode) # print tab; # $a0=$t0; # jump PrintHex (see PrintHex function for that pseudocode) # # $t3 = $t3 + $t0; # # print tab; # $a0=$t3; # # jump PrintDecimal # $a0=$t3; # print tab; # jump PrintHex # print newl; # # } # # $t0=$t2-$t6; # $t1=$t2-$t7; # print mesg1; # print $t7; # print mesg2; # print $t1; # print mesg3; # print $t6; # print mesg4; # print $t0; # print bye; #################################################################### .data #this is my header. I like the term "greetz" better. :) greetz: .asciiz "\n Aligning Numbers Right by Katrina A. \"Kat\" Templeton! \n\n" #prompt: .asciiz "\n Kat says, \"Pick a number, most any number...\" \n" tab: .asciiz "\t" newl: .asciiz "\n" mesg1: .asciiz "\n Number of Positive Values = " mesg2: .asciiz "\n Number of Negative Values = " mesg3: .asciiz "\n Number of even values = " mesg4: .asciiz "\n Number of odd values = " bye: .asciiz "\n AAAAARGH! Kat says that all that effort to right align isn't worth it!" buffer: .asciiz " " buff: .space 13 .globl main .text main: li $t0,0 #initializing $t0 li $v0,4 #system call for Print String la $a0,greetz #reading header into $a0 syscall #print prompt # We're not reading from the keyboard this time around, but # we could very easily change it back so we could. # li $v0,5 #system call for read int. # syscall #reads the value of int to $v0 li $t0,6925 #load the seed (6925) into $t0 li $t1,3021377 #initialize $t1 with the value 3021377 li $t2,32 #initialize $t2 with 16 (for loop counter) #updating to 32 for new program requirements li $t3,0 #initialize $t3 with 0 (for adding all the randoms) loop: mul $t0,$t0,$t1 #multiply $t0 and $t1 together addiu $t0,$t0,5923 #add 5923 to the total in $t0 andi $t5,$t0,1 #checking if we're even or odd beq $t5,1,plusminus #if odd, go on, we can pick up this count later addi $t6,$t6,1 #add one to the evens column plusminus: blez $t0,onward #checking if we're plus or minus. minus, onward addi $t7,$t7,1 #add one to the positive column onward: move $a0,$t0 #move $t0 to $a0 in prep for calling printdec jal printdecimal #call printdecimal # move $a0,$t0 #move $t0 to $a0 in preperation to print an integer # li $v0,1 #system call for Print Integer # syscall #print interger li $v0,4 #system call for print string la $a0,tab #load tab into $a0 syscall #print tab move $a0,$t0 #making it so the printhex function can work its magic... jal printhex #and now we jump to printhex! li $v0,4 #doing it again. System call for print string la $a0,tab #load the tab into $a0 syscall #print tab addu $t3,$t3,$t0 #add $t0 into the running total for $t3. move $a0,$t3 jal printdecimal # move $a0,$t3 #move $t3 into $a0 in prep for printing the integer # li $v0,1 #system call for print integer # syscall #print integer li $v0,4 #doing it again. System call for print string la $a0,tab #load the tab into $a0 syscall #print tab move $a0,$t3 #again, moving the variable so printhex can do its job jal printhex #moving to printhex li $v0,4 #system call for print string la $a0,newl #load newline into $a0 syscall #print newline addi $t2,$t2,-1 #decriment loop by one. bgtz $t2,loop #if $t2>0, then go back to loop and do it again end: li $v0,4 #system call for print string la $a0,bye #load bye prompt into $a0 syscall #print the bye li $v0,10 #system call for ending program syscall #end program ###################################################### #Function Name: PrintHex #Last modified: October 02 9:43 PM ###################################################### #Functional Description of Function: #This program takes a binary value and and prints the #equivalent in hexadecimal #This function requires 85 words of memory #and 85 clock cycles to execute ####################################################### #A binary value is passed to the function in register $a0 # #Calling sequence: # # move $a0,$t0 # jal printhex # #Register usage in Procedure: # $a1 = Address of buffer we're assembling # $s1 = decimal value of '9' in ascii code # $t4 = number of times it takes to dissassemble a # 32 bit number four bits at a time # $t5 = placeholder to do ascii manipulation on those # four bits ########################################################## #Algorithmetic Description in Pseudocode: # $a1 = buffer; # $a1 = $a1 + 10; # $s1 = 57; # $t4 = 8; # # hexloop: # $t5 = and($a0,15) # $t5 = $t5 + 0x30 # if $t5 <= $s1, # goto here # else # $t5 = $t5+7 # # here: # $a1 = store($t5 in $a1) # move to next buffer space in $a1 # $a0 = shift $a0 right by 4 # $t4 = $t4 - 1 # if $t4<0 # goto hexloop # # print buffer # # return to main program ################################################################# printhex: la $a1, buffer #loads buffer into $a1 addi $a1,$a1, 10 #adds ten to that buffer li $s1,57 #load 57 into $s1 li $t4,8 #load 8 into $t4 sb $zero,0($a1) addi $a1,$a1,-1 hexloop: andi $t5, $a0, 15 #make the last four digits fall out addi $t5, $t5, 0x30 #adding the hexadecimal 0x30 to those ble $t5,$s1, here #Branch to here if the decimal is #\_less than 57 (nine in ascii) addi $t5,$t5,7 #add seven to get the proper letter here: sb $t5, 0($a1) #store the hex letter or number in buffer addi $a1,$a1,-1 #move the pointer on buffer srl $a0,$a0,4 #bring the next four bytes to end addi $t4,$t4,-1 #decrement counter bgtz $t4, hexloop #if $t4<0, then do it all over again li $t5,0x78 sb $t5, 0($a1) addi $a1,$a1,-1 li $t5,0x30 sb $t5, 0($a1) addi $a1,$a1,-1 li $t5,0x20 sb $t5, 0($a1) addi $a1,$a1,-1 li $v0,4 #prepping to print the hex number la $a0,buffer #printing the hex number syscall #popping it on screen jr $ra #returning to home base ################################################################### printdecimal: move $t4,$a0 li $t9,0 li $t8,10 bgez $t4,semble li $t9,1 sub $t4,$zero,$t4 semble: la $a0,buff addi $a1,$a0,12 sb $zero,0($a1) shakeout: div $t4,$t4,10 mflo $t4 mfhi $t5 addi $a1,$a1,-1 addi $t5,$t5,48 sb $t5,0($a1) bnez $t4,shakeout negative: beq $t9,0,space addi $a1,$a1,-1 la $t5,45 sb $t5,0($a1) space: bgt $a0,$a1,bugout addi $a1,$a1,-1 la $t5,32 sb $t5,0($a1) b space bugout: li $v0,4 #prepping to print the hex number la $a0,buff #printing the hex number syscall #popping it on screen jr $ra