Document 10834093

advertisement
CSE 305 Introduc0on to Programming Languages Lecture 4 CSE @ SUNY-­‐Buffalo Zhi Yang Courtesy of Google No0ce Board •  Homework2 is posted, it is due on June 17, 2013(Monday). You can schedule your 0me working on it accordingly. •  Homework1 is extended to June 3, 2013 (Monday) 11:59pm. It should be no more extension. Quiz •  Quiz 1 will be given on June 6, 2013
(Thursday). It will take half an hour and cover material from lecture 1 to lecture 5. MIPS product SGI Octane 2 UNIX worksta0on MIPS R10000 Nintendo 64 MIPS R4300 MIPS dies Different dies share similar architecture System Bus Von Neumann computer model.
Address Transla0on Address & Data Buses
2
Register Alloca0on MUX 4
add $s3,$t1,$t2 Branch M
U
X SIGN EXT PC INST REG MemRead M
E
M
MUX ALUSrc Write Reg1 Read Reg2 GENERIC REG FILES MUX RegDst CONTROL Read Reg1 RegWrite ALUOp Mem2Reg Mem write MUX 4
Lw $t2,offset($t1) Branch M
U
X SIGN EXT PC INST REG MemRead M
E
M
MUX ALUSrc Write Reg1 Read Reg2 GENERIC REG FILES MUX RegDst CONTROL Read Reg1 RegWrite ALUOp Mem2Reg Mem write MUX 4
beq $t2,$t1,offset Branch M
U
X SIGN EXT PC INST REG MemRead
M
E
M
MUX ALUSrc Write Reg1 Read Reg2 GENERIC REG FILES MUX RegDst CONTROL Read Reg1 RegWrite ALUOp Mem2Reg Mem write MIPS Memory Usage as viewed in SPIM
MIPS address space 0x7fffffff
0x7fffeffc
0x10010000
reserved
stack segment
data segment
text segment
(instructions)
0x00400000
reserved
0x00000000
Pseudoinstruc0ons • Pseudoinstruc?ons do not correspond to real MIPS instruc0ons. • Instead, the assembler, would translate pseudoinstruc?ons to real instruc0ons (one on more instruc0ons). • Pseudoinstruc?ons not only make it easier to program, it can also add clarity to the program, by making the inten0on of the programmer more clear. Func0on Calls 0x7FFFFFFF
•  In MIPS machines, part of main memory is reserved for a stack. –  The stack grows downward in terms of memory addresses. –  The address of the top element of the stack is stored (by conven0on) in the stack pointer register, $sp. •  MIPS does not provide push and pop instruc0ons. Instead, they must be done explicitly by the programmer. –  So lets see how 0x00000000
stack
A Factorial Example int fact( int n ) { if (( n == 1) || (n==0 )) return (1) else return (fact(n-­‐1)*n); } int main(){ fact(7); return (0); } MIPS Assembly Direc0ves Common Data Defini0ons: • .word w1, ..., wn • store n 32-­‐bit quan00es in successive memory words • .half h1, ..., hn • store n 16-­‐bit quan00es in successive memory halfwords • .byte b1, ..., bn • store n 8-­‐bit quan00es in successive memory bytes • .ascii str • store the string in memory but do not null-­‐
terminate it • strings are represented in double-­‐quotes “str” • special characters, eg. \n, \t, follow C conven0on • .asciiz str • store the string in memory and null-­‐terminate it MIPS Assembly Direc0ves(Cont.) Common Data Defini0ons: • .float f1, ..., fn • store n floa0ng point single precision numbers in successive memory loca0ons • .double d1, ..., dn • store n floa0ng point double precision numbers in successive memory loca0ons • .space n • reserves n successive bytes of space • .align n • align the next datum on a 2n byte boundary. • For example, .align 2 aligns next value on a word boundary. • .align 0 turns off automa0c alignment of .half, .word, etc. 0ll next .data direc0ve MIPS: Software Conventions
MIPS for
Calling C
onven0on Registers
0
zero constant 0
16 s0 callee saves
1
at
...
2
v0 results from callee
23 s7
3
v1 returned to caller
24 t8
4
a0 arguments to callee
25 t9
5
a1
26 k0 reserved for OS kernel
6
a2
27 k1
7
a3
28 gp pointer to global area
8
t0
reserved for assembler
from caller: caller saves
temporary
temporary (cont’d)
29 sp stack pointer
...
30 fp
frame pointer
15 t7
31  ra
return Address
caller saves
Step 1, assign registers •  Let: register 1 = constant 1 register 4 = n register 2 = result •  Given: register 29 = stack pointer ($sp) register 30 = frame pointer ($fp) register 31 = return address ($ra) for procedure/func0on calls Step 2, Store Data .data prompt: .asciiz "Enter a non-­‐nega0ve integer: " endl: .asciiz "\n" Step 3, Write Code • 
• 
.text .globl factorial • 
• 
• 
• 
# Precondi0ons:
# 1st parameter (a0) non-­‐nega0ve integer, n # Postcondi0ons: # result (v0) n factorial • 
• 
• 
• 
• 
• 
factorial: addi $sp, $sp, -­‐8
sw $ra, 0($sp)
li
$v0, 1 beqz $a0, zero
• 
• 
• 
• 
• 
• 
• 
• 
• 
sw
addi
jal
lw
mul
zero: lw
addi
jr
# Make space on stack. # Save return address. # 0! = 1
# Special case for 0! $a0, 4($sp) # Save our argument. $a0, $a0, -­‐1 # Calculate (n-­‐1)! factorial
# Result in v0. $a0, 4($sp) # Restore our argument. $v0, $a0, $v0
# n! = n * (n-­‐1)! $ra, 0($sp) # Restore return address. $sp, $sp, 8 # Restore stack pointer. $ra # Return. .globl main main: addi $sp, $sp, -­‐4
sw
$ra, 0($sp)
la
$a0, prompt li
$v0, 4 syscall li
$v0, 5 syscall move $a0, $v0
jal
factorial move $a0, $v0 li
$v0, 1 syscall la
$a0, endl li
$v0, 4 syscall li
$v0, 0 lw
$ra, 0($sp)
addi $sp, $sp, 4
jr
$ra # Make space on stack. # Save return address. # Display prompt. # Get integer response. # Call factorial func0on. # Print integer result. # Print endl. # Return zero. # Restore return address. # Restore stack pointer. User Stack •  User Stack No?ce: number in square bracket is address Those with blue color are their ascii representa?on •  [7ffffe20]..[80000000] •  [7ffffe20] 00000001 7ffffe5d 00000000 7fffffdd ] ? ?  ! ? ?  [7ffffe30] 7fffffa3 7fffff93 7fffff7f 7fffff72 ᆪ ? ?  モ ? ?   ? ?  r ? ?  [7ffffe40] 7fffff62 7fffff3f 7fffff14 7ffffedf b ? ?  ? ? ?  ? ?  ! ? ?  Stack Trace •  First, every program should have an entry point, for example, QtSpim requires a main label to be declared as global. $sp
0x7ffffe20 main: addi $sp, $sp, -­‐4 # Make space on stack. sw $ra, 0($sp) # Save return address. No0ce: in the beginning, value of sp is 0x7ffffe20 value of ra is 0 $ra = 0x400018
word 1
word 1
$sp
0x7ffffe1c 0x400018
System Call for Input User data segment [10000000]..[10040000] [10000000]..[10010000] are all integer 00000000 [10010000] 65746e45 20612072 2d6e6f6e 6167656e E n t e r a n o n -­‐ n e g a [10010010] 65766974 746e6920 72656765 0a00203a t i v e i n t e g e r : .data prompt: .asciiz "Enter a non-­‐nega0ve integer: “ endl: .asciiz "\n" la $a0, prompt li $v0, 4 syscall # Display prompt. li $v0, 5 syscall # Get integer response. Endianness(Courtesy of Wikipedia) In computing, endian and endianness in the most common cases refer to how bytes are ordered
[1]
within a data item, and endianness is then the same as byte order. A big-endian machine
stores the most significant byte first—at the lowest byte address—while a little-endian machine
stores the least significant byte first.
!
Endian
big
little
!
First byte
(lowest
address)
most significant
least significant
Middle
bytes
...
...
Last byte
(highest
address)
least significant
most significant
Decimal 100000000 (hexadecimal
05F5E100)
Address
Value
n
05
n+1
F5
n+2
E1
n+3
00
Address
Value
n
00
n+1
E1
n+2
F5
n+3
05
Big Endian Machines Tekronix 4051 Graphics Compu0ng System using motorola 68000 microprocessor (1975) An IBM System/360 in use at Volkswagen (1964) Sega Genesis (1988) Mac PowerG5 using PowerPC core (2005) IBM 2094 System z9 (2005) DEC PDP-­‐10 (1968) Li|le Endian Machines Apple II using motorola 6502 (1977) Mac PowerPC G5 using AMD64 core (2005) DEC VAX (1970) SPARC Enterprise M4000 Server (2007) DEC VAX (1977) Microso} Server (2012) Calling Factorial $sp
0x7ffffe1c word 1
move $a0, $v0 # save n to $a0 jal factorial # jump to factorial PC: 0x00400078 No0ce Now :PC= 0x00400024 addi $sp, $sp, -­‐8 # Make space on stack. sw $ra, 0($sp) # Save return address. word 1
0x400018
0x40007c
$sp
0x7ffffe14 Loading Program • 
• 
User Text Segment [00400000]..[00440000] [00400000] 8fa40000 lw $4, 0($29) ; 183: lw $a0 0($sp) # argc [00400004] 27a50004 addiu $5, $29, 4 ; 184: addiu $a1 $sp 4 # argv [00400008] 24a60004 addiu $6, $5, 4 ; 185: addiu $a2 $a1 4 # envp [0040000c] 00041080 sll $2, $4, 2 ; 186: sll $v0 $a0 2 [00400010] 00c23021 addu $6, $6, $2 ; 187: addu $a2 $a2 $v0 [00400014] 0c100015 jal 0x00400054 [main] ; 188: jal main [00400018] 00000000 nop ; 189: nop [0040001c] 3402000a ori $2, $0, 10 ; 191: li $v0 10 [00400020] 0000000c syscall ; 192: syscall # syscall 10 (exit) [00400024] 23bdfff8 addi $29, $29, -­‐8 ; 13: addi $sp, $sp, -­‐8 # Make space on stack. [00400028] a~f0000 sw $31, 0($29) ; 14: sw $ra, 0($sp) # Save return address. [0040002c] 34020001 ori $2, $0, 1 ; 16: li $v0, 1 # 0! = 1 [00400030] 10800006 beq $4, $0, 24 [zero-­‐0x00400030] [00400034] afa40004 sw $4, 4($29) ; 19: sw $a0, 4($sp) # Save our argument. [00400038] 2084ffff addi $4, $4, -­‐1 ; 20: addi $a0, $a0, -­‐1 # Calculate (n-­‐1)! [0040003c] 0c100009 jal 0x00400024 [factorial]; 21: jal factorial # Result in v0. [00400040] 8fa40004 lw $4, 4($29) ; 22: lw $a0, 4($sp) # Restore our argument. [00400044] 70821002 mul $2, $4, $2 ; 23: mul $v0, $a0, $v0 # n! = n * (n-­‐1)! [00400048] 8~f0000 lw $31, 0($29) ; 25: lw $ra, 0($sp) # Restore return address. [0040004c] 23bd0008 addi $29, $29, 8 ; 26: addi $sp, $sp, 8 # Restore stack pointer. [00400050] 03e00008 jr $31 ; 27: jr $ra # Return. Actual Program • 
[00400054] 23bdfffc addi $29, $29, -­‐4 ; 36: addi $sp, $sp, -­‐4 # Make space on stack. [00400058] a~f0000 sw $31, 0($29) ; 37: sw $ra, 0($sp) # Save return address. [0040005c] 3c011001 lui $1, 4097 [prompt] ; 39: la $a0, prompt [00400060] 34240000 ori $4, $1, 0 [prompt] [00400064] 34020004 ori $2, $0, 4 ; 40: li $v0, 4 [00400068] 0000000c syscall ; 41: syscall # Display prompt. [0040006c] 34020005 ori $2, $0, 5 ; 43: li $v0, 5 [00400070] 0000000c syscall ; 44: syscall # Get integer response. [00400074] 00022021 addu $4, $0, $2 ; 46: move $a0, $v0 # Call factorial funcTon. [00400078] 0c100009 jal 0x00400024 [factorial]; 47: jal factorial [0040007c] 00022021 addu $4, $0, $2 ; 49: move $a0, $v0 [00400080] 34020001 ori $2, $0, 1 ; 50: li $v0, 1 [00400084] 0000000c syscall ; 51: syscall # Print integer result. [00400088] 3c011001 lui $1, 4097 [endl] ; 53: la $a0, endl [0040008c] 3424001f ori $4, $1, 31 [endl] [00400090] 34020004 ori $2, $0, 4 ; 54: li $v0, 4 [00400094] 0000000c syscall ; 55: syscall # Print endl. [00400098] 34020000 ori $2, $0, 0 ; 57: li $v0, 0 # Return zero. [0040009c] 8~f0000 lw $31, 0($29) ; 58: lw $ra, 0($sp) # Restore return address. [004000a0] 23bd0004 addi $29, $29, 4 ; 59: addi $sp, $sp, 4 # Restore stack pointer. [004000a4] 03e00008 jr $31 ; 61: jr $ra Factorial (Case n=0) PC: 0x00400024 li
$v0, 1
# 0! = 1 beqz $a0, zero # Special case for 0! sw $a0, 4($sp)
# Save our argument. addi $a0, $a0, -­‐1
# Calculate (n-­‐1)! jal factorial # Result in v0. lw $a0, 4($sp) # Restore our argument. mul $v0, $a0, $v0 # n! = n * (n-­‐1)! zero: # 0x00400048 lw $ra, 0($sp) # Restore return address. addi $sp, $sp, 8 # Restore stack pointer. jr $ra # Return word 1
0x400018
$sp
0x7ffffe14 word 1
$sp 0x7ffffe1c No0ce $ra = 0x0040007c 0x40007c
0x400018
0x40007c
Factorial (Case n=else number, say 5? ) PC: 0x00400024 li
$v0, 1
# 0! = 1 beqz $a0, zero # Special case for 0! sw $a0, 4($sp)
# Save our argument. addi $a0, $a0, -­‐1
# Calculate (n-­‐1)! jal factorial # Result in v0. lw $a0, 4($sp) # Restore our argument. mul $v0, $a0, $v0 # n! = n * (n-­‐1)! zero: # 0x00400048 lw $ra, 0($sp) # Restore return address. addi $sp, $sp, 8 # Restore stack pointer. jr $ra # Return word 1
0x400018
$sp
0x7ffffe14 word 1
$sp 0x7ffffe1c Now $a0 = 4, Then we jump to do factorial again ! ~~~ 0x40007c
0x400018
5
0x40007c
Factorial will finish un0l $a0 = 0 Now $a0 = 1, which is our n $v0 = $a0*$v0= 1*1 = 1 Then we restore return address by adding sp = sp + 8 , and then do the same thing again by using jr $ra PC: 0x00400024 li
$v0, 1
# 0! = 1 beqz $a0, zero # Special case for 0! sw $a0, 4($sp)
# Save our argument. addi $a0, $a0, -­‐1
# Calculate (n-­‐1)! jal factorial # Result in v0. lw $a0, 4($sp) # Restore our argument. mul $v0, $a0, $v0 # n! = n * (n-­‐1)! zero: # 0x00400048 lw $ra, 0($sp) # Restore return address. addi $sp, $sp, 8 # Restore stack pointer. jr $ra # Return word 1
word 1
0x400018
0x400018
5
5
0x40007c
0x40007c
4
0x40007c
4
0x40007c
3
3
0x40007c
0x40007c
2
2
0x40007c
0x40007c
1
1
0x40007c
0x40007c
0
0
0x40007c
0x40007c
$sp
0x7ffffdf8 I/O Management(Courtesy of Dr. Nhut Nguyen) •  In modern computer systems I/O opera0ons are mediated by the OS I/O opera0ons – Mul0ple programs share I/O resources Need protec0on and scheduling – I/O causes asynchronous interrupts Same mechanism as excep0ons – I/O programming is fiddly OS provides abstrac0ons to programs I/O Commands • I/O devices are managed by I/O controller hardware – Transfers data to/from device – Synchronizes opera0ons with so}ware • Command registers – Cause device to do something • Status registers – Indicate what the device is doing and occurrence of errors • Data registers – Write: transfer data to a device – Read: transfer data from a device How To Operate on I/O Registers? • Memory mapped I/O – Registers are addressed in same space as memory – Address decoder dis0nguishes between them – OS uses address transla0on mechanism to make them only accessible to kernel – Example: MIPS • I/O instruc0ons and I/O ports – Separate instruc0ons to access I/O ports – Can only be executed in kernel mode – Example: x86 (IN and OUT instruc0ons on I/O segment) When to Perform I/O Opera0ons ? Easy Way -­‐ Polling • Periodically check I/O status register – If device ready, do opera0on – If error, take ac0on • Common in small or low-­‐performance real0me embedded systems – Predictable 0ming – Low hardware cost • In other systems, wastes CPU 0me When to Perform I/O Opera0ons? Be|er Way -­‐ Interrupts •  When a device is ready or error occurs – Controller interrupts CPU •  Interrupt is like an excep0on – But not synchronized to instruc0on execu0on – Can invoke handler between instruc0ons – Cause informa0on o}en iden0fies the interrup0ng device •  Priority interrupts – Devices needing more urgent a|en0on get higher priority – A higher priority I/O device can interrupt handler for a lower priority I/O device I/O Data Transfer – How? •  Programmed I/O – CPU transfers data between memory and I/O data registers – Time consuming for high-­‐speed devices •  Direct memory access (DMA) – OS provides star0ng address in memory – I/O controller transfers to/from memory autonomously – Controller interrupts on comple0on or error MIPS-­‐ Memory-­‐Mapped I/O • The MIPS I/O address space 0xffff0000 to 0xffffffff is reserved for memory-­‐mapped I/O – How large is the I/O address space? • Any address in this space can be mapped to I/O registers (command, status, data) of an I/O device • I/O is performed using the regular load and store instruc?ons on these addresses – e.g. li $t0, 0xffff0004 lw $s1, ($t0) MIPS I/O Example !"#$."/0.12+',3&
MIPS
CPU
0x00000000-0xfffeffff
Main Memory
Interrupt Enable
Ready
0xffff0000
Receiver Control
Keyboard
0xffff0004
Receiver Data
0xffff0008
Transmitter Control
Display
0xffff000c
Transmitter Data
8 bits
18
Keyboard & Display Controller !"#$%&'()*%+,'%--"')Ȃ *).&+/0&/
Keyboard Control
0xFFFF0000
2 bits
Keyboard Data
0xFFFF0004
8 bits
Display Control
0xFFFF0008
2 bits
Display Data
0xFFFF000C
8 bits
!"#$%%$&#'(!" )*+,'-%.
*+/#0,/&%123,42&'%,20 5$&!*6
$&!* 7%%89::::88886
;",/&%))$&!*<8=%>%?-%77%8-%@5%A
Computer Buses –  A number of buses are in widespread use in the computer world. •  Mul0bus (8086) •  IBM PC (PC/XT) •  ISA bus (PC/AT) •  EISA bus (80386) •  Microchannel (PS/2) •  PCI bus (Many PCs) •  Nubus (macintosh) •  Universal Serial Bus (modern PCs) •  FireWire (consumer electronics) 
Download