Skip to content

Instantly share code, notes, and snippets.

@montycheese
Created November 5, 2015 01:04
Show Gist options
  • Save montycheese/62fb45bec18f70213001 to your computer and use it in GitHub Desktop.
Save montycheese/62fb45bec18f70213001 to your computer and use it in GitHub Desktop.
Assembly!
.data
str1: .asciiz "Enter input string: "
getc1: .asciiz "Enter char to replace: "
getc2: .asciiz "Enter char to replace with: "
ret1: .asciiz "Original string: "
ret2: .asciiz "Substitute "
ret3: .asciiz "Result String: "
arrow: .asciiz " -> "
buffer: .space 100 #space to store our string, char input[100]
buf2: .space 3 #space to store char and '\0'
buf3: .space 3 #space to store char and '\0'
.text
.globl main
#$t0->user str, $t1->old char, $t2->new char, $t3 tem str, $t4 temp char, $t5 temp char
main:
#put new item on stack
addi $sp, $sp, -4 #adjust stack for 1 item
sw $s0, 0($sp)
add $s0, $zero, $zero #i=0
la $a0, str1 #load message to argument register
li $v0, 4 #send message (stored in arg register) to stdout
syscall # printf("Enter input string: ")
li $v0, 8 #read string from stdin, scanf("%s", input)
la $a0, buffer #load byte space into addr
li $a1, 100 #create byte space for string
move $t0, $a0 #save string to t0 store users input into $t0
syscall
la $a0, getc1 #load next message to arg register
li $v0, 4 #send message to stdout
syscall #printf("enter char: ")
#actually reading a string to escape newline then parsing the char
li $v0, 8 #read char from stdin, scanf("%s", char1)
la $a0, buf2 #load byte space into addr
li $a1, 3 #create byte space for char
move $t3, $a0 #save for char in string to t1 store users input into $t1
syscall
#take char from str
add $t4, $s0, $t3 #load first item in buf2
lbu $t1, 0($t4) #put char at array[i] in $t1
la $a0, getc2 #load next message to arg register
li $v0, 4 #send message to stdout
syscall #printf("enter replacement char: ")
#actually reading a string to escape newline then parsing the char
li $v0, 8 #read char from stdin, scanf("%s", char1)
la $a0, buf3 #load byte space into addr
li $a1, 3 #create byte space for char
move $t3, $a0 #save for char in string to t1 store users input into $t1
syscall
#take replacement char from str
add $t4, $s0, $t3 #load first item in buf2
lbu $t2, 0($t4) #put char at array[i] in $t1
la $a0, ret1 #load ret1 to print "Original String: "
li $v0, 4 #send to stdout
syscall
#print our original string
la $a0, buffer #reload byte space to primary addr
move $a0, $t0 #primary address = t0 address (load pointer)
li $v0, 4 #print out string
syscall
la $a0, ret2 #load ret2 to print "Substitute: "
li $v0, 4 #send to stdout
syscall
la $a0, 0($t1) #load $t1 to print old char
li $v0, 11 #send to stdout
syscall
#la $a0, buf2 #load char1
#li $v0, 4 #send to stdout
#syscall
la $a0, arrow #load arrow to print " -> "
li $v0, 4 #send to stdout
syscall
la $a0, 0($t2) #load $t2 to print new char
li $v0, 11 #send to stdout
syscall
addi $a0, $0, 0xA #print newline
addi $v0, $0, 0xB #syscall 11 prints the lower 8 bits of $a0 as an ascii character.
syscall
la $a0, ret3 #load ret3 to print "Result string: "
li $v0, 4 #send to stdout
syscall
loop:
add $t4, $s0, $t0 #load first item in buffer
lbu $t5, 0($t4) #put char at array[i] in $t5
addi $s0, $s0, 1
beq $t1, $t5, then #check our input char with array[i]
beq $t5, $zero, results #if current char is equal to NULL continue to results
j loop
then:
sb $t2, 0($t4) #replace curr char with replacement char
j loop #return to loop
results:
#print our modified string
la $a0, buffer #reload byte space to primary addr
move $a0, $t0 #primary address = t0 address (load pointer)
li $v0, 4 #print out string
syscall
li $v0, 10 #kill program
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment