-
-
Save aflaag/181d644faf0726d32ffe041a7b75a28e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .globl main | |
| .data | |
| W: .asciiz "+6-4+3-2" | |
| .text | |
| main: | |
| la $a0, W # a0 = W_address | |
| jal calc | |
| move $a0, $v0 # a0 = v0 | |
| li $v0, 1 # print int | |
| syscall | |
| li $v0, 10 # exit | |
| syscall | |
| calc: | |
| # push stack | |
| subi $sp, $sp, 12 | |
| sw $a0, 8($sp) | |
| sw $fp, 4($sp) | |
| sw $ra, 0($sp) | |
| # index | |
| # helpers | |
| move $t2, $zero # t2 = 0, used to store the value of the current number | |
| # result | |
| move $v0, $zero # v0 = 0 | |
| # conditions | |
| addi $s0, $s0, '-' | |
| addi $s1, $s1, '+' | |
| addi $s2, $s2, 1 | |
| loopString: | |
| lb $t1, 0($a0) # t1 = mem[a0], the current character | |
| beq $t1, $zero, exitLoopString # exit if the current character is the null char \0 | |
| beq $t1, $s1, dontSetFlag | |
| beq $t1, $s0, setFlag | |
| subi $t2, $t1, 0x30 # t2 = t1 - 0x30, because 0x3 is the ascii value for '0' | |
| beq $t3, $s2, invertIndex # if the flag is set, invert the index | |
| beq $t3, $zero, updateTotal # if the flag is not set, just update the total | |
| invertIndex: | |
| sub $t2, $zero, $t2 # t2 = 0 - t2, invert the current value | |
| j updateTotal | |
| updateTotal: | |
| add $v0, $v0, $t2 # v0 += t2, update the current total | |
| j loopEnd | |
| loopEnd: | |
| add $a0, $a0, 1 # a0 += 1 | |
| j loopString | |
| exitLoopString: | |
| # pop stack | |
| lw $ra, 0($sp) | |
| lw $fp, 4($sp) | |
| lw $a0, 8($sp) | |
| addi $sp, $sp, 12 | |
| jr $ra | |
| dontSetFlag: | |
| move $t3, $zero | |
| j loopEnd | |
| setFlag: | |
| move $t3, $zero | |
| addi $t3, $t3, 1 | |
| j loopEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment