Skip to content

Instantly share code, notes, and snippets.

@giuscri
Created June 29, 2017 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuscri/ccea8dfc3b3d3083fc3df4890c27ba56 to your computer and use it in GitHub Desktop.
Save giuscri/ccea8dfc3b3d3083fc3df4890c27ba56 to your computer and use it in GitHub Desktop.
.data
hi: .asciiz "aabbaa"
.text
main:
sub $sp, $sp, 4
sw $ra, 0($sp)
sub $sp, $sp, 4
sw $fp, 0($sp)
move $fp, $sp
sub $sp, $sp, 4
la $t0, hi
sw $t0, 0($sp)
jal is_palindrome
add $sp, $sp, 4
move $sp, $fp
lw $fp, 0($sp)
add $sp, $sp, 4
lw $ra, 0($sp)
add $sp, $sp, 4
jr $ra
is_palindrome:
sub $sp, $sp, 4
sw $ra, 0($sp)
sub $sp, $sp, 4
sw $fp, 0($sp)
move $fp, $sp
sub $sp, $sp, 4
sw $s0, 0($sp)
li $s0, 0 # s0 := <len of string>
lw $t0, 8($fp)
lb $t1, 0($t0)
find_len_loop:
beqz $t1, end_find_len_loop
add $s0, $s0, 1
add $t0, $t0, 1
lb $t1, 0($t0)
j find_len_loop
end_find_len_loop:
li $v0, 0
li $t0, 0
sub $t1, $s0, 1
is_palindrome_loop:
bge $t0, $t1, end_is_palindrome_loop
lw $t2, 8($fp)
add $t2, $t2, $t0
lb $t2, 0($t2)
lw $t3, 8($fp)
add $t3, $t3, $t1
lb $t3, 0($t3)
bne $t2, $t3, return_is_palindrome
add $t0, $t0, 1
sub $t1, $t1, 1
j is_palindrome_loop
end_is_palindrome_loop:
li $v0, 1
return_is_palindrome:
lw $s0, 0($sp)
add $sp, $sp, 4
lw $fp, 0($sp)
add $sp, $sp, 4
lw $ra, 0($sp)
add $sp, $sp, 4
move $sp, $fp
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment