Last active
August 29, 2015 14:09
-
-
Save cjlarose/87ddb22b1efa27ac6677 to your computer and use it in GitHub Desktop.
MIN_INT fail
This file contains 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
void printint(int); | |
int f() { | |
int n; | |
n = -2147483648; | |
printint(n); | |
return n; | |
} |
This file contains 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
# void printint(int x) : prints a single int | |
# void printchar(char c) : prints a single char | |
# void printdouble(double d) : print a single double | |
# int toint(double d) : floor of number, then convert to int | |
# double todouble(int i) : convert int to a double | |
.text | |
printint: | |
li $v0, 1 # system call code for print_int | |
syscall # print it | |
jr $ra # return to caller's code | |
printchar: | |
li $v0, 11 # system call code for print_char | |
syscall # print it | |
jr $ra # return to caller's code | |
printdouble: | |
l.d $f12, 0($a0) # copy words at a0 and a0+4 to $f12 and $f13 | |
li $v0, 3 # system call code for print_double | |
syscall # print it | |
jr $ra # return to caller's code | |
toint: | |
l.d $f12, 0($a0) # copy words at a0 and a0+4 to $f12 and $f13 | |
floor.w.d $f12, $f12 # compute the floor (as a word), store in $f12 | |
mfc1 $v0, $f12 # move f12 in fp coproc to v0 | |
jr $ra # return to caller's code | |
todouble: | |
mtc1 $a0, $f14 # move the int in a0 to f12 | |
cvt.d.w $f14, $f14 # convert f12 from int to double | |
mfc1 $v0, $f14 | |
mfc1 $v1, $f15 | |
jr $ra | |
.data | |
.text | |
f: | |
subu $sp, $sp, 24 | |
sw $ra, 4($sp) | |
sw $fp, 0($sp) | |
addiu $fp, $sp, 20 | |
addi $t0, $zero, -2147483648 | |
sw $t0, 4($fp) # temp0 = $t0 | |
lw $t0, 4($fp) # $t0 = temp0 | |
neg $t2, $t0 | |
sw $t2, 0($fp) # temp1 = $t2 | |
lw $t4, 0($fp) # $t4 = temp1 | |
sw $t4, 8($fp) # n = $t4 | |
lw $t0, 8($fp) # $t0 = n | |
move $a0, $t0 | |
sw $t0, 0($sp) | |
jal printint | |
lw $v0, 8($fp) # $v0 = n | |
j f_epilogue | |
f_epilogue: | |
lw $ra, 4($sp) | |
lw $fp, 0($sp) | |
addiu $sp, $sp, 24 | |
jr $ra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment