Presentation is loading. Please wait.

Presentation is loading. Please wait.

System Calls & Arithmetic

Similar presentations


Presentation on theme: "System Calls & Arithmetic"— Presentation transcript:

1 System Calls & Arithmetic
Updated 10/12/2010

2 System Calls Print string Print integer Read integer

3 Print String Example # HELLO WORLD .data
# Symbolic label Hello: data type is ASCII string Hello: .asciiz "Hello\n" .text # Text segment is unused for now. main: # Start of code section # Load register $v0 with code 4 for print string. li $v0, 4 # Load $a0 with address of string. # Label names are case sensitive. la $a0, Hello # call operating system to perform print operation syscall # END OF PROGRAM - MUST LEAVE ONE BLANK LINE AT END.

4 Printing an Integer Value
Use a MIPS system call to print the value of a 32-bit integer # system call code for print_int = 1 li $v0, 1 # put integer value in register $a0 li $a0, 75 syscall

5 Register-to-Register Move
Use the move instruction to copy a value from one register into another Assign register $a0 the same value as $t0 # $a0 = $t0 move $a0, $t0

6 Printing an Integer Value
Print the value in a register # system call code for print_int = 1 li $v0, 1 # suppose the value to print is in $t3 # Assign $a0 the value contained in $t3 move $a0, $t3 syscall

7 Print Integer Example # PRINT AN INTEGER .data
.text # Text segment is unused for now. main: # Start of code section # Load register $v0 with code 1 for print integer li $v0, 1 # Load $a0 with the integer value. li $a0, 75 # call operating system to perform print operation syscall # END OF PROGRAM - MUST LEAVE ONE BLANK LINE AT END.

8 Programming Exercise Enhance your Find Exponent program so that it prints the message: The exponent is X Where X is the value of the actual unbiased power of 2 exponent left in a register

9 Read Integer Example # READ AN INTEGER .data
.text # Text segment is unused for now. main: # Start of code section # Load register $v0 with code 5 for read integer li $v0, 5 # call operating system to do read integer syscall # Integer value is returned in register $v0 # END OF PROGRAM - MUST LEAVE ONE BLANK LINE AT END.

10 Read & Echo Print Integer
# READ AN INTEGER AND ECHO PRINT ITS VALUE. .data Prompt:.asciiz “Enter an integer: “ Echo: .asciiz “Your number is “ .text # Text segment is unused for now. main: # Print prompt message to enter an integer. li $v0, 4 la $a0, Prompt syscall # Load register $v0 with code 5 for read integer li $v0, 5 # Integer value is returned in register $v0 move $t0, $v0 # Continued...

11 Read & Echo Print Integer
# ...CONTINUED: # Print echo message. li $v0, 4 la $a0, Echo syscall # Load register $v0 with code 1 for print integer li $v0, 1 # Put saved input integer value in register $a0 move $a0, $t0 # End of program

12 Arithmetic Instructions
Integer multiply Integer divide

13 Integer Multiply Example # Load operands for multiplication li $v0, 5 li $v1, 3 # $a0 = $v0 x $v1 mul $a0, $v0, $v1

14 Integer Multiply (big #s)
Multiply two 32-bit integers Product is a 64-bit integer MIPS processor provides two special registers called HI and LO to store the product

15 Integer Multiply # Set operands. li $t0, 7 li $t1, 11 # multiply $t0 x $t1 mult $t0, $t1 # Save lower 32-bits of product to $t2 mflo $t2 # Save upper 32-bits of product in $t3 mfhi $t3

16 Integer Multiply Example
# Set operands li $t0, 7 $t0 = 0x li $t1, 11 $t1 = 0x B # Compute 7 x 11 mult $t0, $t1 # Product in special registers LO = 0x D (77 base 10) HI = 0x

17 BIG Integer Multiply Example
# Set both operands 1,000,000 li $t0, $t0 = 0x000F4240 li $t1, $t1 = 0x000F4240 # Compute 1,000,000 x 1,000,000 mult $t0, $t1 # Product is more than 32-bits! LO = 0xD4A5100 HI = 0x000000E8

18 Integer Division Example # Load operands for division li $v0, 15 li $v1, 3 # $a0 = $v0 / $v1 div $a0, $v0, $v1

19 Integer Divide (dual action)
Divide two 32-bit integers Quotient is stored in the LO register Remainder is left in the HI register

20 Integer Division # Set operands. li $t0, 25 li $t1, 3 # Compute $t0 / $t1 div $t0, $t1 # Save quotient to register $t2 mflo $t2 # Save remainder to register $t3 mfhi $t3

21 Integer Division Example
# Set operands li $t0, 25 $t0 = 0x li $t1, 3 $t1 = 0x # Compute 25 / 3 div $t0, $t1 LO = 0x (Quotient) HI = 0x (Remainder)

22 Cycles Per Instruction
MIPS integer multiply takes 5-12 clock cycles MIPS integer divide takes clock cycles Bitwise shift instructions execute in only one clock cycle!

23 Programming Exercise Create string prompt for user and a string output as a label quarters: . asciiz "Quarters: " Prompt user to enter number of pennies 1-99 Compute & print number of quarters needed to make change (fewest coins) output the label with the value– test it and get it working before proceeding.. Compute & print number of dimes – test it Compute & print number of nickels – test is Example: 67 cents Quarters: 2 Dimes: Nickels: 1 Pennies: 2


Download ppt "System Calls & Arithmetic"

Similar presentations


Ads by Google