Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIPS Programing Model 및 프로그램

Similar presentations


Presentation on theme: "MIPS Programing Model 및 프로그램"— Presentation transcript:

1 MIPS Programing Model 및 프로그램
Hoon Oh

2 MIPS Memory Layout Memory organization
Memory consists of a number of cells, each of which holds one byte. Memory cells are numbered sequentially from zero up to the maximum allowable size. Address space Text segment holds instructions for a program Data segment Static data Dynamic data Grows upward after static data. Stack Resides in the top of the address space and grows downward.

3 (data that instructions
MIPS Memory Layout ($sp) 0x7fffffff Stack segment 0x 0x00 0x 0xA0 0x 0x3E 0x 0x10 Dynamic data 0xFFFFFFFC 0x90 ($gp) 0x 0xFFFFFFFD 0x6F Data segment (data that instructions operate on) 0xFFFFFFFE 0xA1 0xFFFFFFFF 0x00 0x Text segment (Instructions) Memory Organization ($pc) 0x reserved MIPS Memory Layout

4 MIPS registers 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), $k0 (26) and $k1 (27) are reserved for use by the assembler and OS. $v0 and $v1 (2, 3) are used for return values. $t0 - $t9 (8-15, 24, 25) are used for temporary values. $s0 - $s7(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.

5 MIPS Registers Register name Number Usage zero constant 0 at 1
constant 0 at 1 reserved for assembler v0, v1 2 ~ 3 expression evaluation and results of a function a0 ~ a3 4 ~ 7 arguments 1 - 4 t0 ~ t7 8 ~ 15 temporary (not preserved across call) s0 ~ s7 16 ~ 23 saved (preserved across call) t8, t9 24, 25 temporary (not preserved across call) k0, k1 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)

6 SPIM Simulator SPIM Simulator that runs programs written for MIPS R2000/R3000 processors Advantages to using a machine simulator MIPS workstation are not generally available. 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. Disadvantages The programs run slower than on a real machine.

7 OS system calls 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 Kernel Hardware Programs make system calls asking kernel to do I/O Kernel controls I/O devices directly

8 SPIM’s system calls SPIM provides a small set of 10 OS-like system services through the system call (syscall) instruction. Service Call code Arguments Results print_int 1 $a0 = integer print_float 2 $f12 = float print_double 3 $f12 = double print_string 4 $a0 = string read_int 5 integer (in $v0) read_float 6 float (in $f0) read_double 7 double (in $f0) read_string 8 $a0 = buffer, $a1 = length sbrk 9 $a0 = amount address (in $v0) exit 10 $v0 : system call code $a0...$a3 : arguments ($f12 <- floating point values) syscall (initiate system service) $v0 :results ($f0: for the floating point)

9 Code Format Source code format
[label:] operation [operand], [operand], [operand] [#comment] Brackets ([ ]) indicates an optional field. Constants Hex numbers: prefix with 0x String: “hello world\n” Special characters: \n (newline), \t (tab), \” (quote). SPIM directives for character strings .ascii “abcd” : do not null-terminate them. .asciiz “abcd”: null-terminate them.

10 Code Format: hello.a ## hello.a – prints out “hello world”
## a0 – points to the string ################################################################### # text segment # .text .globl __start __start: # execution starts here la $a0, str # put string address into a0 li $v0, 4 # system call to print syscall # out a string li $v0, 10 syscall # au revoir ... # data segment # .data str: .asciiz “hello world\n” ## ## end of file hello.a

11 PC-SPIM for Windows Registers window Text segment Data Segment
Messages window

12 Inside text & data segments
[0x ] 0x3c lui $1, 4097 [str] ;9: la $a0, #str put string .. [0x ] 0x ori $4, $1, 0 [str] [0x ] 0x ori $2, $0, 4 ;10: li $v0, #system call to .. [0x c] 0x c syscall ;11; syscall [0x ] 0x a ori $s2, $0, 10 ;13; li $v0, 10 [0x ] 0x c syscall ;14; syscall text segment DATA [0x ] ... [0x000fffx] 0x [0x1000fffc] 0x [0x ] 0x6c6c6568 0x6f77206f 0x0a646c72 0x [0x ] ... [0x ] 0x STACK [0x7fffaffc] 0x data segment

13 MIPS 산술연산 예 Instruction Example Comments Add add $t1, $t2, $t3
Add immediate addi $t1, $t2, 50 # $t1 = $t2 + 50 Subtract sub $t1, $t2, $t3 # $t1 = $t2 - $t3 Multiply mult $t2, $t3 # Hi, Lo = $t2 x $t3 Divide div $t2, $t3 # Lo = $t2 / $t3 # Hi = $t2 mod $t3 Move from Hi mfhi $t1 # $t1 = Hi # get copy of Hi Move from Li mflo $t1 # $t1 = Lo # get copy of Lo

14 Programming Example: temp.a
## temp.a ask user for temperature in Celsius. ## convert to Fahrenheit, print the result. (F = 9C/5 + 32) ## v0 – reads in Celsius ## t0 – holds Fahrenheit result ## a0 – points to output strings ############################################################ # text segment # .text .globl __start __start: la $a0, prompt # print prompt on terminal li $v0,4 syscall li $v0,5 # syscall 5 reads an integer mul $t0,$v0,9 # to convert,multiply by 9, div $t0,$t0,5 # divide by 5, then add $t0,$t0,32 # add 32

15 temp.a (cont...) la $a0,ans1 # print string before result li $v0,4
syscall mov $a0,$t0 # print result move $a0,endl # system call to print li $v0,4 # out a newline li $v0,10 syscall # au revoir ... ############################################################## # data segment # .data prompt: .asciiz “Enter temperature (Celsius): “ ans1: .asciiz “The temperature in Fahrenheit is “ end1: .asciiz “\n“ ## end of file temp.a

16 math1.a ## Question: calculate A * X^2 + B * X + C
## Output format: "answer =180" ############################################################## # text segment # .text .globl __start __start: # execution starts here # 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 math1.a

17 Solution: math1.a # start cut # This solution has a bug. Perform source level debugging to # single step through the code and locate the error. lw $t0, X lw $t1, A lw $t2, B lw $t3, C mul $t4, $t0, $t0 # t4 = X^2 mul $t4, $t4, $t1 # t4 = A * X^2 mul $t5, $2, $t0 # t5 = B * X add $t4, $t4, $t5 # t4 = A * X^2 + B * X add $t4, $t4, $t3 # t4 = A * X^2 + B * X + C la $a0, ans # system call to print li $v0, 4 # out string syscall move $a0, $t4 # print result on terminal li $v0, 1 move $a0, endl # system call to print li $v0, 4 # out a newline li $v0, 10 syscall # au revoir ... # end cut

18 실습1 프로그램 math1.a의 해답에서 버그가 하나 존재한다. Step button을 사용하여 해답의 5번째 라인 (lw $t1, A) 이 수행된 후에 다음 레지스터의 내용을 체크하시오. PC, t0, t1, gp, sp 10번째 라인 (mul $t5, $2, $t0)이 수행되기 전 후에 다음 레지스터들의 내용을 체크하시오. PC t4, t5 실행 후에 t5의 내용이 B*X=4 x 7 = 28 (0x1C)인가? 에러를 수정한 후에 값이 180이 나오는 지를 체크 하시오. 다음을 계산하는 프로그램을 작성하고 실행하여 다음시간까지 제출하시오. 5 * X^2 – 3 X값은 7로 하시오. 계산 결과는 242입니다.

19 과제1 Machine 언어와 Asembly언어의 관계는 무엇인가?
일반 C프로그래밍에 비해 Assembly프로그램의 장.단점은 무엇인가? 컴퓨터가 5에서 3을 뺄 때 거치는 과정을 보이시오? RISC 프로세서의 기본적인 아이디어는 무엇인가? Program counter의 목적은 무엇인가? SPIM에서 Identifier (식별자) 를 만드는 룰은 무엇인가? Syntax error와 logical error의 차이점은 무엇인가? Single stepping의 목적은 무엇인가? Breakpoint는 무엇인가? hi 와 lo 레지스터는 무슨 목적으로 사용되는가? Assembly programming에서 comments와 indentation이 왜 그렇게 중요한가? la, lb, li, 그리고 lw 사이의 차이점은 무엇인가? 산술적/논리적인 명령의 Operand는 어떤 순서로 놓여져야 하는가?


Download ppt "MIPS Programing Model 및 프로그램"

Similar presentations


Ads by Google