Assembly Programming MIPS Programing Model Hoon Oh UOU
Assembly Programming MIPS Programing Model 및 프 로그램 Hoon Oh UOU 1 -1
Assembly Programming MIPS Memory Layout n Memory organization l Memory consists of a number of cells, each of which holds one byte. l Memory cells are numbered sequentially from zero up to the maximum allowable size. n Address space l Text segment ü holds instructions for a program l Data segment ü Static data ü Dynamic data Ø Grows upward after static data. l Stack ü Resides in the top of the address space and grows downward. UOU 1 -2
Assembly Programming MIPS Memory Layout ($sp) 0 x 7 fffffff 0 x 00000001 0 x. A 0 0 x 00000002 0 x 3 E 0 x 00000003 0 x 10 0 x. FFFFFFFC 0 x 90 0 x. FFFFFFFD 0 x 6 F 0 x. FFFFFFFE 0 x. A 1 0 x. FFFF 0 x 00 Memory Organization Stack segment Dynamic data ($gp) 0 x 10008000 0 x 10000000 ($pc) 0 x 00400000 Data segment (data that instructions operate on) Text segment (Instructions) reserved MIPS Memory Layout UOU 1 -3
Assembly Programming MIPS registers n n n n MIPS processor contains 32 general purpose registers (numbered 0 – 31). Register n is designated by $n or Rn. $zero (0) is always set to 0. $at (1), $k 0 (26) and $k 1 (27) are reserved for use by the assembler and OS. $v 0 and $v 1 (2, 3) are used for return values. $t 0 - $t 9 (8 -15, 24, 25) are used for temporary values. $s 0 - $s 7(16 -23) are used for long-lived values. $gp, $sp, $fp, $ra are used for special purposes, as shown in the table of the next page. UOU 1 -4
Assembly Programming MIPS Registers Register name Number Usage zero 0 constant 0 at 1 reserved for assembler v 0, v 1 2~3 expression evaluation and results of a function a 0 ~ a 3 4~7 arguments 1 - 4 t 0 ~ t 7 8 ~ 15 temporary (not preserved across call) s 0 ~ s 7 16 ~ 23 saved (preserved across call) t 8, t 9 24, 25 temporary (not preserved across call) k 0, k 1 26, 27 reserved for OS kernel gp 28 pointer for global area sp 29 stack pointer fp 30 frame pointer ra 31 return address (used by function call) UOU 1 -5
Assembly Programming SPIM Simulator n SPIM l Simulator that runs programs written for MIPS R 2000/R 3000 processors n Advantages to using a machine simulator l MIPS workstation are not generally available. l Provides better environment for low-level programming than an actual machine: ü detect more errors ü provide more features ü give convenient edit-assemble-load development cycle, compared to using a circuit board. ü easy to identify and fix the bugs. n Disadvantages l The programs run slower than on a real machine. UOU 1 -6
Assembly Programming OS system calls n n An application program asks the kernel to do I/O by making system calls. The kernel implements these system calls by talking directly to the hardware. Application programs Programs make system calls asking kernel to do I/O Kernel controls I/O devices directly Hardware UOU 1 -7
Assembly Programming SPIM’s system calls n SPIM provides a small set of 10 OS-like system services through the system call (syscall) instruction. Service Call code Arguments print_int 1 $a 0 = integer print_float 2 $f 12 = float print_double 3 $f 12 = double print_string 4 $a 0 = string read_int 5 integer (in $v 0) read_float 6 float (in $f 0) read_double 7 double (in $f 0) read_string 8 $a 0 = buffer, $a 1 = length sbrk 9 $a 0 = amount exit 10 UOU Results address (in $v 0) $v 0 : system call code $a 0. . . $a 3 : arguments ($f 12 <- floating point values) syscall (initiate system service) $v 0 : results ($f 0: for the floating point) 1 -8
Assembly Programming Code Format n n n Source code format [label: ] operation [operand], [operand] [#comment] Brackets ([ ]) indicates an optional field. Constants l Hex numbers: prefix with 0 x l String: “hello worldn” l Special characters: n (newline), t (tab), ” (quote). l SPIM directives for character strings. ascii “abcd” : do not null-terminate them. . asciiz “abcd”: null-terminate them. UOU 1 -9
Assembly Programming Code Format: hello. a 1 2 3 4 5 6 7 8 9 10 11 ## hello. a – prints out “hello world” ## a 0 – points to the string ################################## # text segment # ##################################. text. globl __start: # execution starts here la $a 0, str # put string address into a 0 li $v 0, 4 # system call to print syscall # out a string 12 13 14 15 16 17 18 19 20 21 li $v 0, 10 syscall # au revoir. . . ################################## # data segment # ##################################. data str: . asciiz “hello worldn” ## ## end of file hello. a UOU 1 -10
Assembly Programming PC-SPIM for Windows ·Registers window ·Text segment ·Data Segment ·Messages UOU window 1 -11
Assembly Programming Inside text & data segments [0 x 00400000] [0 x 00400004] [0 x 00400008] [0 x 0040000 c] [0 x 00400010] [0 x 00400014] 0 x 3 c 011001 0 x 34200000 0 x 34020004 0 x 0000000 c 0 x 3402000 a 0 x 0000000 c lui $1, 4097 [str] ; 9: la $a 0, #str put string. . ori $4, $1, 0 [str] ori $2, $0, 4 ; 10: li $v 0, #system call to. . syscall ; 11; syscall ori $s 2, $0, 10 ; 13; li $v 0, 10 syscall ; 14; syscall text segment DATA [0 x 10000000] [0 x 1000 fffc] [0 x 10010000] [0 x 10010010] . . . [0 x 000 fffx] 0 x 00000000 0 x 6 c 6 c 6568 0 x 6 f 77206 f 0 x 0 a 646 c 72 0 x 0000. . . [0 x 10020000] 0 x 0000 STACK [0 x 7 fffaffc] 0 x 0000 data segment UOU 1 -12
Assembly Programming MIPS 산술연산 예 Instruction Example Comments Add add $t 1, $t 2, $t 3 # $t 1 = $t 2 + $t 3 Add immediate addi $t 1, $t 2, 50 # $t 1 = $t 2 + 50 Subtract sub $t 1, $t 2, $t 3 # $t 1 = $t 2 - $t 3 Multiply mult $t 2, $t 3 # Hi, Lo = $t 2 x $t 3 Divide div $t 2, $t 3 # Lo = $t 2 / $t 3 # Hi = $t 2 mod $t 3 Move from Hi mfhi $t 1 # $t 1 = Hi # get copy of Hi Move from Li mflo $t 1 # $t 1 = Lo # get copy of Lo UOU 1 -13
Assembly Programming Example: temp. a 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. ## temp. a ask user for temperature in Celsius. ## convert to Fahrenheit, print the result. (F = 9 C/5 + 32) ## v 0 – reads in Celsius ## t 0 – holds Fahrenheit result ## a 0 – points to output strings ############################## # text segment # ##############################. text. globl __start: la $a 0, prompt # print prompt on terminal li $v 0, 4 syscall li $v 0, 5 # syscall 5 reads an integer syscall mul $t 0, $v 0, 9 # to convert, multiply by 9, div $t 0, 5 # divide by 5, then add $t 0, 32 # add 32 UOU 1 -14
Assembly Programming temp. a (cont. . . ) 20. 21. 22. la $a 0, ans 1 li $v 0, 4 syscall # print string before result mov $a 0, $t 0 li $v 0, 4 syscall # print result move $a 0, endl li $v 0, 4 syscall # system call to print # out a newline 23. 24. 25. 26. 27. 28. 29. 30. 31. li $v 0, 10 syscall # au revoir. . . 32. 33. 34. 35. 36. 37. ############################### # data segment # ############################### 38. 39. 40. 41. 42. 43. . data prompt: . asciiz “Enter temperature (Celsius): “ ans 1: . asciiz “The temperature in Fahrenheit is “ end 1: . asciiz “n“ ## end of file temp. a UOU 1 -15
Assembly Programming math 1. a 1. 2. 3. 4. 5. 6. 7. 8. ## Question: calculate A * X^2 + B * X + C ## Output format: "answer =180" ############################### # text segment # ###############################. text. globl __start: # execution starts here 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. # Any changes outside of two dashed lines will be discarded # by mipsmark. put your answer between dashed lines. # --------- start cut -------------(해답) # --------- end cut --------------############################### # data segment # ###############################. data X: . word 7 A: . word 3 B: . word 4 C: . word 5 ans: . asciiz “answer = “ endl: . anciiz “n” ## End of file math 1. a 1 -16 UOU
Assembly Programming Solution: math 1. a 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. # --------- start cut -------------# This solution has a bug. Perform source level debugging to # single step through the code and locate the error. lw $t 0, X lw $t 1, A lw $t 2, B lw $t 3, C mul $t 4, $t 0 # t 4 = X^2 mul $t 4, $t 1 # t 4 = A * X^2 mul $t 5, $2, $t 0 # t 5 = B * X add $t 4, $t 5 # t 4 = A * X^2 + B * X add $t 4, $t 3 # t 4 = A * X^2 + B * X + C 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. la $a 0, ans # system call to print li $v 0, 4 # out string syscall move $a 0, $t 4 # print result on terminal li $v 0, 1 syscall move $a 0, endl # system call to print li $v 0, 4 # out a newline syscall li $v 0, 10 syscall # au revoir. . . # --------- end cut --------------UOU 1 -17
- Slides: 19