Skip to content

Instantly share code, notes, and snippets.

@khult
Created June 22, 2015 23:47
Show Gist options
  • Save khult/be93748f751e1d12e992 to your computer and use it in GitHub Desktop.
Save khult/be93748f751e1d12e992 to your computer and use it in GitHub Desktop.
Assembly Language - note some programs are not 100% functional
##Kristofer Hult
##Homework 3
##Part 2
##Compute e to the x
.data
str1: .asciiz "Give a number X "
nline: .asciiz "\n"
result1: .asciiz " e raised to equals"
.text
.globl main
main:
subu $sp,$sp,4 # push return address
li $v0, 4 #Prompt for X
la $a0,strl
sw $f0,$a0
j exp
Print:
li $v0,4 # print "exp(x)"
la $a0,result1
syscall
li $v0,2 # print float
la $a0,$f12 #prints result located in $f12
syscall
li $v0,4 # print /n
la $a0,nline
syscall
j exit
exp:
li.s $f2, 1.0 # current term, x*n/n!
li.s $f3, 3.0 # n <-- 3.0
li.s $f12,1.0 # sum <-- 1.0 (== term)
li $t0,0 # start count at 0
j loop
loop:
beq $t0,11,done # for ( count=0; count<11; count++ )
mul.s $f2,$f2,$f0 # term *= x
div.s $f2,$f2,$f3 # term /= n
add.s $f12,$f12,$f2 # sum += term
add.s $f3,$f3,$f1 # n += 1.0
addiu $t0,$t0,1 # count++
j loop # jump to begining of loop
done:
j Print # prints result
exit:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
##Kristofer Hult
##Homework 3
##Part 1 Int Factorial
##Int Factorial will return the factorial of n when given float x, int n
.data
nEqual: .asciiz "Enter n = "
newLine: .asciiz "\n"
.text
main:
la $a0, nEqual # print "Enter n = "
li $v0, 4 #
syscall #
li $v0, 5 # read integer
syscall #
move $a0, $v0 # $a0 = $v0
jal fact # $v0 = fib(n)
move $a0, $v0 # $a0 = fib(n)
li $v0, 1 # print int
syscall #
la $a0, newLine # print "\n"
li $v0, 4 #
syscall #
fact:
slti $t0, $a0, 2 # if i < 2 (i.e i == 1)
beq $t0, $zero, cont # if i >= 2 go to cont
addi $v0, $zero, 1 # else make the return value 1
jr $ra
cont:
#save into stack
addi $sp, $sp, -8 # make space in the stack
sw $ra, 0($sp) # save the return address
sw $a0, 4($sp) # save the argument value
addi $a0, $a0, -1 # compute fact(n - 1)
jal fact
lw $ra, 0($sp) # get old return address from stack
lw $a0, 4($sp) # get old argument value from stack
addi $sp, $sp, 8 # return stack pointer to original value, thus erasing all values
mult $v0, $a0 # multiply n * fib(n - 1)
mflo $v0 # gets the result of the multiplication from the low register
jr $ra #return
#Kristofer Hult
#Problem 3 HW 1
#This program will promt user for a value of n and then tell user what that letter is.
.data
prompt: .asciiz "\n Please input a value of 1 thru 26 for N = " #ask user to input a value for N
array1: .byte 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' #creates an array of the alphabet
message: .word 'The Letter is:'
.text
main:
li $s5, prompt #stores prompt value in $s5
la $t0, array1 #load base address of array into register $t0
la $a0, message #stores the message into $a0 for printing
loop:
#$s3=i
sll $t1,$s3,2 #temp reg $t1 = i*4
add $t1,$t1,s$6 #$t1 = address of array1[i]
lw $t0,0($t1) #temp reg $t0=array1[i]
beq $t0,$s5, Else #go to else if array1[i] = value given for n
addi $s3,$s3,1 #i=i+1
j Loop #go to Loop
Else:
add $s1, $a0, $t0 #message + array1[i]
li $v0, 4 # load string print system call code into register $v0;
la $a0, $s1 # load address of string to be printed into $a0
syscall # call system call for printing string
exit:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
#Kristofer Hult
#Problem 1 HW 1
#This program will loop thru an array and print out it's contents
.data
array1: .word '9','72','101','108','108','111','13','10','0' #Soring given array
.text
main:
li $s5, 0 #stores final value in $s5
la $t0, array1 # load base address of array into register $t0
loop:
#$s3=i
sll $t1,$s3,2 #temp reg $t1 = i*4
add $t1,$t1,s$6 #$t1 = address of array1[i]
lw $t0,0($t1) #temp reg $t0=array1[i]
la $a0, $t0 #loads array
li $v0, 11 # System call code for Print Char
syscall # Print the characters of the array
beq $t0,$s5, Exit #go to exit if array1[i] = 0
addi $s3,$s3,1 #i=i+1
j Loop #go to Loop
exit:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
#Problem 1, Homework 2
#Kristofer Hult
#this program will prompt for 2 integers and multiply them using addition
#and a loop
.data
userX: .asciiz "Enter a number for x between 0 and 255, x= "
userY: .asciiz "Enter a number for y between 0 and 255, y= "
Check: .word 0 #creates check variable
output: .asciiz "Product = "
newLine: .asciiz "\n" # new line
.text
main:
li $v0,4 # print string
la $a0,userX # a0 is having the string user
syscall
li $v0, 5 #read int
move $t0, $v0 #load given X to $t0
li $v0,4 # print string
la $a0, newLine # load new line
syscall
li $v0,4 # print string
la $a0,userY # a0 is having the string user
syscall
li $v0, 5 #read int
move $t1, $v0 #load given y to $t1
lw $s1, check # load check to $s1
li $v0,4 # print string
la $a0, newLine # load new line
syscall
li $v0, 4 # print string
la $a0, output
syscall
loop:
add $t1, $t0, $t1 # $t1=X+Y
addi $s1, $s1, 1 # adds 1 to check
beq $s1, $t0, exit # check condition, break if check = X
j loop
exit:
li $v0, 1 # print string
la $a0, $t1
syscall
li $v0, 10 # exit system call
syscall
#Problem 2, Homework 2
#Kristofer Hult
#this program will take 2 arrays of size 8 and multiply their elements together
.data
Array1: .word 9,13,10,20,1,6,9,14,0 # array of numbers
Array2: .word 41,3,5,7,19,2,1,7,0 #array of numbers
Array3: .space40 #creates a 40-element character array to store the result. this is to make sure there is enough room for the result
Null: .word 0 # creates null
Check: .word 0 #creates check variable
newLine: .asciiz "\n" # new line
.text
main: lw $s1, Null # load Null to $s1
la $t0, Array1 # load address of A1 to $t0
la $t1, Array2 # load address of A2 to $t1
la $t4, Array3
lw $s1, check # load check to $s1
loop:
lw $t2, 0($t0) # load the next value of A1 into $t2
lw $t3, 0($t1) # load the next value of A2 into $t3
beq $t2, $s1, exit # if value of A1 = Null exit
j multiLoop
multiLoop:
add $t3, $t2, $t3 # $t1=A1+A2
addi $s1, $s1, 1 # adds 1 to check
beq $s1, $t2, resetCheck # check condition, break if check = A1 value
j multiLoop
resetCheck:
sw $s1, $s1, Null #sets check to 0
sw $t3, 0($t4) #stores result in Array3
li $v0, 4 # print string system call
la $a0, $t4 # load Array3 result
syscall
addi $t0, $t0, 4 # increment word for Array1
addi $t1, $t1, 4 # increment word for Array2
addi $t4, $t4, 4 # increment word for Array3
j loop #returns to loop
exit: li $v0, 10 # exit system call
syscall
##Kristofer Hult
##Homework 3
##Part 1 Float Power and Int Factorial
##Float Power returns x to the nth power
##Int Factorial will return the factorial of n when given float x, int n
.data
str1: .asciiz "Give a number X from 1 to 20 "
str2: .asciiz "Give integer n from 0 to 5 "
errormsg: .asciiz "Out of range.\n"
nline: .asciiz "\n"
result1: .asciiz " raised to "
result2: .asciiz " gives: "
.text
error:
li $v0,4 #print str1
la $a0,errormsg
syscall
beq $s2,$zero,getX
j getn
.globl main
main:
addi $s0,$zero,21 #s0=21
addi $s1,$zero,6 #s1=6
getX:
addi $s2,$zero,0 #s2=0 to input x and 1 to input n
li $v0,4
la $a0,str1
syscall #print str1
li $v0,5
syscall #read int
slt $s3,$v0,$s0 #$s3=($v0<$s0) if(x<21) $s1=1
beq $s3,$zero,error #if $s3=0 goto error
blez $v0,error #if ($v0<=0) goto error
move $t0,$v0
getn:
addi $s2,$zero,1
li $v0,4
la $a0,str2
syscall #print string2
li $v0,5
syscall #read int
slt $s3,$v0,$s1 #$s3=($v0<$s1) if(x<6) $s3=1
beq $s3,$zero,error #if $s3=0 goto error
bltz $v0,error #if ($v0<0) goto error
move $t1,$v0
beq $t1,$zero,else #if (t1=0) t2=1, t1=n,t2=result
addi $t2,$zero,1
addi $s4,$zero,0
loop: #if s4<t1
slt $s5,$s4,$t1 #$s5=($s4<$t1) if(x<21) $s5=1
beq $s5,$zero,printresult
#if $s1=0 goto printresult(s4=t1)
mul $t2,$t2,$t0 #t2=t2*t0
addi $s4,$s4,1
j loop
else:
addi $t2,$zero,1
j printresult
printresult:
li $v0,1
move $a0,$t0
syscall #print X
li $v0,4
la $a0,result1
syscall #print " raised to "
li $v0,1
move $a0,$t1
syscall #print n
li $v0,4
la $a0,result2
syscall #print " gives "
li $v0,1
move $a0,$t2
syscall #print result(t2)
li $v0,10
syscall #exit
#Kristofer Hult
#Problem 1 HW 1
#This program will print my name, Kristofer Hult using a system call
.data
name1: .word KristoferHult #store name to be printed out
.text
main:
li $v0, 4 # load string print system call code into register $v0;
la $a0, name1 # load address of string to be printed into $a0
syscall # call system call for printing string
exit:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
#Problem 3, Homework 2
#Kristofer Hult
#removes white space from a given input string
.data
str: .space 81 # buffer for input string
strNS: .space 81 # buffer for string without spaces
prompt: .asciiz "Enter a string up to 64 characters\n"
head1: .asciiz "\nOriginal String: "
head2: .asciiz "\nWith spaces removed: "
.text
main:
#print the first prompt and get the input string from console
li $v0, 4 #load syscall value to print string into $v0
la $a0, prompt #address of prompt to print
syscall #print prompt to console
li $v0, 8 #load syscall value to read input string
la $a0, str #addr of allocated space for input string is now in $a0
li $a1, 81
syscall
jal loop
addi $t1, $v0, 0 #the count of spaces is in $v0, save it into $t1
li $v0, 4 #print header then the count
la $a0, head1
syscall
la $a0, str #print the original string
syscall
li $v0, 4
la $a0, head2 #print the second header
syscall
la $a0, strNS #print no spaces string
syscall
End:
li $v0, 10 #load syscall value for exit
syscall #exit
loop:
add $t1, $t3, $t4 #$t1 = addr of str[i]
lb $t2, 0($t1) #$t2 = character in str[i]
beq $t2, $zero, exitCS #break from loop if $t2 contains null character
addi $a0, $t2, 0 #place value to be checked in $a0
#save values onto stack from temp registers to preserve them
addi $sp, $sp, -28 #adjust the stack pointer for 5 values
sw $t6, 24($sp) #save index of string with no spaces
sw $t5, 20($sp) #save addr of string with no spaces
sw $t4, 16($sp) #save index of user input
sw $t3, 12($sp) #save the addr of user input
sb $t2, 8($sp) #save the character in str[i]
sw $t1, 4($sp) #save the address of str[i]
sw $t0, 0($sp) #save the count of spaces
jal isWhiteSpace #result from this jump and link will be in $v0 after call
#pop saved values from the stack, then reset the pointer
lw $t6, 24($sp)
lw $t5, 20($sp)
lw $t4, 16($sp)
lw $t3, 12($sp)
lb $t2, 8($sp)
lw $t1, 4($sp)
lw $t0, 0($sp)
addi $sp, $sp, 28 #reset stack pointer
beq $v0, $zero, addChar #if not a space, continue to next character
addi $t0, $t0, 1 #if it is a space, increment count
addChar:
bne $v0, $zero, nextChar #If character is a space, break
sll $t7, $t6, 2 #index if nospaces string stores width of 4
add $t7, $t7, $t5 #now $t7 points at nospaces[i]
sb $t2, 0($t7) #store the character in the nospaces string
addi $t6, $t6, 1 #increment the index of nospaces
nextChar:
addi $t4, $t4, 1 #increment the index value
j loop #jump back to loop and continue processing
exitCS:
addi $v0, $t0, 0 #count of spaces placed into $v0
addi $v1, $t5, 0
lw $ra, 4($sp) #load return addr from the stack
lw $a0, 0($sp) #load value to check from the stack
addi $sp, $sp, 8 #reset stack pointer
jr $ra #return
isWhiteSpace:
addi $sp, $sp, -12 #adjust stack pointer to make room
sw $s0, 8($sp)
sw $ra, 4($sp) #store value of return addr onto stack
sw $a0, 0($sp) #store value to check onto stack
#Check to see if the character is a space
li $t0, 32 #ascii value for space character loaded into $t0
li $v0, 0 #Set default return to 0, or "not a space character"
bne $t0, $a0, endSC #if ascii values match, character is a space
li $v0, 1 #$v0 = 1 means it is a space character
endSC:
lw $s0, 8($sp)
lw $ra, 4($sp) #restore return address
lw $a0, 0($sp) #restore addr of str
addi $sp, $sp, 12 #reset the stack pointer
end: jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment