Float Operations We will write a full program to read in two floats, perform an add, subtract, multiply, and divide, printing out each result Let s attempt in C (using blocking IO)
Float Operations We will write a full program to read in two floats, perform an add, subtract, multiply, and divide, printing out each result Let s attempt in C (using blocking IO) float a, b; scanf( %f, &a); scanf( %f, &b); printf( a + b = %f\n, a + b); printf( a b = %f\n, a b); printf( a b = %f\n, a b); printf( a / b = %f\n, a / b);
Float Operations float a, b; scanf( %f, &a); scanf( %f, &b); printf( a + b = %f\n, a + b); printf( a b = %f\n, a b); printf( a b = %f\n, a b); printf( a / b = %f\n, a / b);
Float Operations float a, b; scanf( %f, &a); scanf( %f, &b); li $v0, 6 # problem? li $v0, 6 # read into f0 # read into f0 printf( a + b = %f\n, a + b); printf( a b = %f\n, a b); printf( a b = %f\n, a b); printf( a / b = %f\n, a / b); add.s $f12, $f0, $f1 # and print sub.s $f12, $f0, $f1 # and print mult.s $f12, $f0, $f1 # and print div.s $f12, $f0, $f1 # and print
Convert To Float.data endl:.asciiz "\n" addmsg:.asciiz "a + b = " submsg:.asciiz "a - b = " mulmsg:.asciiz "a * b = " divmsg:.asciiz "a / b = ".text.globl main main: li $v0, 6 # enter a float mov.s $f1, $f0 li $v0, 6 # enter a float add.s $f12, $f0, $f1 la $a0, addmsg mfc1 $a1, $f12 jal printop sub.s $f12, $f0, $f1 la $a0, submsg mfc1 $a1, $f12 jal printop mul.s $f12, $f0, $f1 la $a0, mulmsg mfc1 $a1, $f12 jal printop div.s $f12, $f0, $f1 la $a0, divmsg mfc1 $a1, $f12 jal printop # add and print # sub and print # multiply and print # add and print exit: li $v0, 10 # exit # printop: print corresponding message, result, and newline # input: # a0 = &msg # a1 = result (float) # outputs: None printop: li $v0, 4 # print message (string) mfc1 $a1, $f12 li $v0, 2 # print result (float) la $a0, endl li $v0, 4 # print endl jr $ra
Convert to Float We will write a full program to read in nonblocking input and convert it to float Let s attempt in C (using blocking IO)
Convert to Float We will write a full program to read in nonblocking input and convert it to float Let s attempt in C (using blocking IO) while(tmp == getchar()) { if(tmp < 0 ) { printf( %d too low\n, tmp); else if(tmp > 9 ) { printf( %d too high\n, tmp); int intval = (int) tmp; float floatval = (float) intval; printf( %f, floatval); break;
Convert to Float while(tmp == getchar()) { if(tmp < 0 ) { printf( %d too low\n, tmp); else if(tmp > 9 ) { printf( %d too high\n, tmp); int intval = (int) tmp; float floatval = (float) intval; printf( %f, floatval); break;
Convert to Float while(tmp == getchar()) { if(tmp < 0 ) { printf( %d too low\n, tmp); else if(tmp > 9 ) { printf( %d too high\n, tmp); int intval = (int) tmp; float floatval = (float) intval; printf( %f, floatval); break; li $t0, 48 blt $s6, $t0, toolow li $t0, 57 bgt $s6, $t0, toohigh addi $s6, $s6, -48 mtc1 $s6, $f12 cvt.s.w $f12, $f12
.data endl:.asciiz "\n" lowmsg:.asciiz " is too low" highmsg:.asciiz " is too high".text.globl main main: mfc0 $a0, $12 # read from the status register ori $a0, 0xff11 # enable all interrupts mtc0 $a0, $12 # write back to the status register lui $t0, 0xFFFF ori $a0, $0, 2 sw $a0, 0($t0) li $s6, 10000 li $s7, 10000 ascii code loop: beq $s6, $s7, loop move $a0, $s6 # $t0 = 0xFFFF0000 # enable keyboard interrupt # write back to 0xFFFF0000 # $s6 used to pass the ascii code # a large number impossible to be an # if no input, loop li $t0, 48 # if (input < 48) goto loop blt $s6, $t0, toolow li $t0, 57 # if (input > 57) goto loop bgt $s6, $t0, toohigh addi $s6, $s6, -48 mtc1 $s6, $f12 cvt.s.w $f12, $f12 # convert char to int # convert int to float li $v0, 2 # print input la $a0, endl loopreset: mfc0 $t0, $12 # Set Status register andi $t0, 0xfffe # clear ENABLE mtc0 $t0, $12 # write back to status li $s6, 10000 mfc0 $t0, $12 ori $t0, 1 mtc0 $t0, $12 j loop toolow: li $v0, 11 li $v0, 4 la $a0, lowmsg la $a0, endl j loopreset toohigh: li $v0, 11 li $v0, 4 la $a0, highmsg la $a0, endl j loopreset exit: li $v0, 10.kdata s1:.word 10 s2:.word 11.ktext 0x80000180 # $s0 used to pass the ascii code # Set Status register # set ENABLE # write back to status # print low message # print high message # exit,if it ever comes here # kernel data # kernel code starts here sw $v0, s1 # To show how to save, we use these registers sw $a0, s2 # not using the stack because the interrupt might be # triggered by a memory reference # using a bad value of the stack pointer mfc0 $k0, $13 # Cause register srl $a0, $k0, 2 # Extract ExcCode Field (bits 2-6) andi $a0, $a0, 0x1f bne $a0, $zero, kdone processing I/O here lui $v0, 0xFFFF lw $s6, 4($v0) kdone: lw $v0, s1 lw $a0, s2 # Exception Code 0 is I/O. Only # $v0 = 0xFFFF0000 # get the input key # Restore other registers mtc0 $0, $13 # Clear Cause register mfc0 $k0, $12 # Set Status register andi $k0, 0xfffd # clear EXL bit ori $k0, 0x11 # Interrupts enabled mtc0 $k0, $12 # write back to status eret # return to EPC Convert To Float