1 / 21

Chap.2: Instructions: Language of the computer

Chap.2: Instructions: Language of the computer. Jen-Chang Liu, Spring 200 6 Adapted from http://www-inst.eecs.berkeley.edu/~cs61c/. Example for Lab#1: 印出 sum=5. .globl main main: .data str: .asciiz "sum=" .text li $v0, 4 # $v0=4 la $a0, str # $a0=str

takoda
Download Presentation

Chap.2: Instructions: Language of the computer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap.2:Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from http://www-inst.eecs.berkeley.edu/~cs61c/

  2. Example for Lab#1: 印出 sum=5 .globl main main: .data str: .asciiz "sum=" .text li $v0, 4 # $v0=4 la $a0, str# $a0=str syscall # print the string li $s0, 2 li $s1, 3 add $a0, $s0, $s1 # 2+3=5 li $v0, 1 # print integer syscall

  3. 不要亂寫 assembly code • add $s0, 1

  4. Pseudo-instruction • SPIM can recognize a large instruction set than the original MIPS • Example: li (load immediate) SPIM Pseudo-instruction MIPS Bare machine real inst.

  5. Shut down pseudo-instruction option

  6. Outline • C operators, operands • Variables in Assembly: Registers • Comments in Assembly • Addition and Subtraction in Assembly • Memory Access in Assembly 指令

  7. Assembly Operands: Memory • C variables map onto registers; what about large data structures like arrays? • 1 of 5 components of a computer: memory contains such data structures • But MIPS arithmetic instructions only operate on registers, never directly on memory. • Data transfer instructions transfer data between registers and memory: • Memory to register: load • Register to memory: store

  8. MIPS CPU store load

  9. MIPS memory allocation (Textbook Fig2.17) • 32 bits addressing • one byte in an address 20000000 1 byte It is software convention

  10. Data Transfer: Memory to Reg (2/4) • To specify a memory address to copy from, specify two things: • A register which contains a pointer to memory • A numerical offset (Max. offset???) • Example: 8($t0) • specifies the memory address pointed to by the value in $t0, plus 8 bytes • The desired memory address is the sum of these two values, 8+$t0.

  11. Memory access: 8($t0) Pointer $t0 (register) Offset 8 (constant) Start of an array 8 (constant) Offset $t0 (register)

  12. Data Transfer: Memory to Reg (3/4) • Load Instruction Syntax: 1 2,3(4) • where 1) operation name 2) register that will receive value 3) numerical offset in bytes 4) register containing pointer to memory • Instruction Name: • lw (meaning Load Word, so 32 bits(4 bytes) or one word are loaded at a time) Example: lw $t0,12($s0)

  13. Example: load word

  14. Data Transfer: Reg to Memory (1/2) • Also want to store value from a register into memory • Store instruction syntax is identical to Load instruction syntax • Instruction Name: sw (meaning Store Word, so 32 bits or one word are loaded at a time)

  15. Data Transfer: Reg to Memory (2/2) • Example: sw $t0,12($s0) This instruction will take the pointer in $s0, add 12 bytes to it, and then store the value from register $t0 into the memory address pointed to by the calculated sum

  16. Pointers v.s. Values • Key Concept: A register can hold any 32-bit value.(typeless) • That value can be a (signed) int, an unsigned int, a pointer (memory address), etc. • If you write add $t2,$t1,$t0 then $t0 and $t1 better contain values • If you write lw $t2,0($t0) then $t0 better contain a pointer • Don’t mix these up!

  17. C: word address => assembly: byte address • What offset in lw to select A[8] in C? • A is a 4-byte type (ex. long int) • Compile by hand using registers:g = h + A[8]; • g: $s1,h: $s2,$s3:base address of A • 4x8=32 bytes offset to select A[8] • add $s1, 32($s3) lw $t0,32($s3) # $t0 gets A[8] add $s1,$s2,$t0 # $s1 = h+A[8]

  18. 0 1 2 3 Aligned Not Aligned More Notes about Memory: Alignment • MIPS requires that all words start at addresses that are multiples of 4 bytes • Called Alignment: objects must fall on address that is multiple of their size.

  19. Role of Registers vs. Memory • What if more variables than registers? • Compiler tries to keep most frequently used variable in registers • Why not keep all variables in memory? • Smaller is faster:registers are faster than memory • Registers more versatile: • MIPS arithmetic instructions can read 2, operate on them, and write 1 per instruction • MIPS data transfer only read or write 1 operand per instruction, and no operation

  20. Brief summary (1/2) • In MIPS Assembly Language: • Registers replace C variables • One Instruction (simple operation) per line • Simpler is Better • Smaller is Faster • Memory is byte-addressable, but lw and sw access one word at a time. • A pointer (used by lw and sw) is just a memory address, so we can add to it or subtract from it (using offset).

  21. Brief summary (2/2) • New Instructions: add, addi, sub lw, sw • New Registers: C Variables: $s0 - $s7 Temporary Variables: $t0 - $t9 Zero: $zero

More Related