From 52495eef498fd8aa964e31f19279786b34be673c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 26 Jun 2017 13:56:29 +0200 Subject: [PATCH] Add palidrome detection exercice. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TD4/mips3.asm | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 TD4/mips3.asm diff --git a/TD4/mips3.asm b/TD4/mips3.asm new file mode 100644 index 0000000..889258c --- /dev/null +++ b/TD4/mips3.asm @@ -0,0 +1,111 @@ +.data +input: .space 20 +input_string: .asciiz "Saisir un mot ? " +msg_palindrome_part_1: .asciiz "Le mot " +msg_palindrome_part_2: .asciiz " un palindrome" +msg_is_palindrome: .asciiz "est" +msg_is_not_palindrome: .asciiz "n'est pas" +newline: .asciiz "\n" + +.text +main: + jal input_word + jal clean_and_length_word + jal isPalindrome + li $v0, 10 + syscall + +input_word: + la $a0, input_string + li $v0, 4 + syscall + li $v0, 8 + la $a0, input + li $a1, 20 + syscall + jr $ra + +clean_and_length_word: + la $s4, ($zero) + la $s0, input # $s0 contains base address of the string buffer + addi $s3, $0, '\n' # $s3 = '\n' +loop1: + lb $s1, 0($s0) # load character into $s1 + beq $s1, $s3, end1 # break if byte is newline + addi $s0, $s0, 1 # increment buffer address + addi $s4, $s4, 1 + j loop1 +end1: + la $a2, ($s4) + jr $ra + +isPalindrome: + # Check base case + slti $t0, $a2, 2 + bne $t0, $zero, returnTrue + + # Make sure first and last are equal + lb $t0, 0($a0) + addi $t1, $a2, -1 + add $t1, $t1, $a0 + lb $t1, 0($t1) + bne $t0, $t1, returnFalse + + # Shift pointer, length, recurse + addi $a2, $a2, -2 + addi $a0, $a0, 1 + j isPalindrome + +returnFalse: + addi $v0, $zero, 0 + addi $sp, $sp, -4 + sw $ra, 0($sp) + jal display_not_palindrome + lw $ra, 0($sp) + addi $sp, $sp, 4 + jr $ra + +returnTrue: + addi $v0, $zero, 1 + addi $sp, $sp, -4 + sw $ra, 0($sp) + jal display_palindrome + lw $ra, 0($sp) + addi $sp, $sp, 4 + jr $ra + +display_palindrome: + la $a0, msg_palindrome_part_1 + li $v0, 4 + syscall + la $a0, input + li $v0, 4 + syscall + la $a0, msg_is_palindrome + li $v0, 4 + syscall + la $a0, msg_palindrome_part_2 + li $v0, 4 + syscall + la $a0, newline + li $v0, 4 + syscall + jr $ra + +display_not_palindrome: + la $a0, msg_palindrome_part_1 + li $v0, 4 + syscall + la $a0, input + li $v0, 4 + syscall + la $a0, msg_is_not_palindrome + li $v0, 4 + syscall + la $a0, msg_palindrome_part_2 + li $v0, 4 + syscall + la $a0, newline + li $v0, 4 + syscall + jr $ra \ No newline at end of file -- 2.34.1