#This program demonstrates math operations in MIPS. #author: Hornick #CS2710 - 1/5/2013 .text #.text directive (default) just tells the assembler where to put the instructions in memory main: #this label is assigned the value of the address of the next instruction addi $t1, $zero, 0x7FFFFFFF # largest positive signed int addi $t2, $zero, 1 # t2=1 #signed/unsigned addition addu $t3, $t2, $t1 # unsigned addition; no exception generated on overflow # add $t3, $t2, $t1 # signed addition; exception generated (when sign of result is opposite that of operands) #integer multiplication addi $t1, $zero, 0x7FFFFFFF # largest positive signed int addi $t2, $zero, 32 # t2=32 mult $t1, $t2 # 64-bit product in $hi/$lo registers mul $s3, $t1, $t2 # most significant bits in $hi; # least significant 32-bits in $lo and $s3 mfhi $s0 # move 32-bit $hi to $s0 mflo $s1 # move 32-bit $lo to $s1 #integer division with finite operands addi $t1, $zero, 17 # t1=17 addi $t2, $zero, 5 # t2=5 div $t2, $t1 # 5/17, quotient (0) in $lo, remainder (5) in $hi div $t1, $t2 # 17/5, quotient (3) in $lo, remainder (2) in $hi #integer division with 0 operands addi $t1, $zero, 0 # t1=0 addi $t2, $zero, 5 # t2=5 div $t2, $t1 # 5/0, error (programmer must check for 0 denominator) - no change to $hi or $lo div $t1, $t2 # 0/5, quotient (0) in $lo, remainder (0) in $hi #floating-point multiplication lwc1 $f0, x # load f.p. value from memory at x lwc1 $f1, y # load f.p. value from memory at y add.s $f12, $f0, $f1 # add the values addi $v0, $zero, 2 #opcode for print float syscall #print the float value in $f12 lwc1 $f0, big #load a big value add.s $f12, $f0, $f0 #should overflow addi $v0, $zero, 2 #opcode for print float syscall #print the float value in $f12 lwc1 $f0, large #load a large value mul.s $f12, $f0, $f0 #should overflow addi $v0, $zero, 2 #opcode for print float syscall #print the float value in $f12 lwc1 $f0, x #load a finite value lwc1 $f1, zero #load a zero div.s $f12, $f0, $f1 #x/0 - overflows addi $v0, $zero, 2 #opcode for print float syscall #print the float value in $f12 lwc1 $f1, zero #load a zero div.s $f12, $f1, $f1 #0/0 - undefined addi $v0, $zero, 2 #opcode for print float syscall #print the float value in $f12 addi $v0, $zero, 17 # syscall opcode for "exit" addi $a0, $zero, 0 # exit status code (similar to java exit(0) syscall # exit with status code 0 .data .float x: 1.0, -1.0, 0, y: 3.625, z: -3.625, 12 4.625 biggest: 3.4028234E38 # approximate largest floating point value (single precesion) smallest: 1.1754943E-38 # approximate smallest floating point value big: 2.9E38 large: 1E20 zero: 0 .double xx: 1.0, -1.0, 0, yy: 3.625 zz: -3.625, 12