180 likes | 282 Views
Chapter 2. Instructions: Language of the Computer Part II. Review:. MIPS Assembly Language: Registers replace C++ variables Saved values: $s0 - $s7 Temporary values: $t0 - $t9 One Instruction (simple operation) per line Design Principles Simplicity favors regularity Smaller is faster.
E N D
Chapter 2 Instructions: Language of the Computer Part II
Review: • MIPS Assembly Language: • Registers replace C++ variables • Saved values: $s0 - $s7 • Temporary values: $t0 - $t9 • One Instruction (simple operation) per line • Design Principles • Simplicity favors regularity • Smaller is faster Florida A & M University - Department of Computer and Information Sciences
Memory Operands • When more than eight (8) variables • Store some of them in memory • Main memory also used for data structures • Arrays, structures, dynamic data • MIPS arithmetic instructions only operate on registers, never directly on memory • Data transfer instructions transfer data between memory and registers • Memory to register • Register to memory Florida A & M University - Department of Computer and Information Sciences
... 24 4 bytes of data 20 4 bytes of data 16 4 bytes of data 12 4 bytes of data 8 4 bytes of data 4 4 bytes of data 0 4 bytes of data Memory Organization (words) • Viewed as a large, single-dimension array where consecutive elements differ by 4 bytes • address it by supplying a pointer to (address of) a memory location • Word addresses: • 0: word 1 • 4: word 2 (after the 4 bytes of word 1) • 8: word 3 (after the 8 bytes of words 1-2) • 4*n: word n+1 (after n words of 4 bytes) Florida A & M University - Department of Computer and Information Sciences
Memory Operands • To apply arithmetic operations • Load values from memory into registers • Perform arithmetic using registers • Store result from register to memory • Memory is byte-addressed • Byte addresses have no alignment restriction • Each byte address identifies an 8-bit entity • Word address must be a multiple of 4 • Double word address must be a multiple of 8 • Half word address must be a multiple of 2. Florida A & M University - Department of Computer and Information Sciences
Byte Order with a Word • Big Endian LRbyte order • Most-significant byte at lowest byte address • “BOYZ” | B | O | Y | Z | 08 09 10 11 address • Little Endian R Lbyte order • Least-significant byte at lowest byte address • “BOYZ” | Z | Y | O | B | 08 09 10 11 address • MIPS is Big Endian Florida A & M University - Department of Computer and Information Sciences
MIPS A/L for Data Access float Z; // 1 word data area. char C; // 1 byte data area. MIPS instructions: To allocate space for variables C and Z. .data Z: .word C: .byte To load values of variables C and Z into registers $t2 and $t3 lb $t2, C lw $t3, Z To store byte value into C, and word value into Z sb $t1, C sw $t4, Z Florida A & M University - Department of Computer and Information Sciences
Addressing Scalar Variables • A scalar variable contains only ONE value • Declared in the MIPS .data section myNum: .word yourChar: .byte • Can/should be addressed/accessed BY NAME lw $t1, myNum sb $s4, yourChar • Can be accessed indirectly via a base register la $s0, myNum #-| $s0 contains @myNum lw $t1, 0 ($s0) #-| use addr = 0 + $s0 • ALWAYS: offset = 0 for scalars Florida A & M University - Department of Computer and Information Sciences
Data Addressing Modes • All data memory references go through a register, called a base register • Load the address into a base register Example: la $t4, myData la $t3, yourID • Access using the base register Example: lw $s3, 0($t4) # load value of myData sw $s2, 0($t3) # store value into yourID • Address used = base + offset (=0 for scalars) Florida A & M University - Department of Computer and Information Sciences
Determining the Offset • base – address of first byte of data area • offset– the byte location of desired data within the data area = #bytes beyond the start of data area. • Offset depends on the data structure: • Scalars offset = 0 • struct: • First field offset = 0 • Second field offset = #bytes in first field • N’th field offset = #bytes in first N-1 fields • Array of M-byte elements (for int, M is 4) • A[0] offset =0 • A[k]= offset = k*M (#bytes in elements 0,1,…,k-1 ) Florida A & M University - Department of Computer and Information Sciences
Accessing structured Data (struct) struct myRec { float fees; int hours; float GPA; }; myRec drj; . . . • .data • drJ: .space 12 # 0:fees, 4:hours, 8:GPA . . . • la $t1, drj # @ of rec in base reg $t1. • sw $t5, 0($t1) # save fees. • sw $t3, 4($t1) # save hours. • sw $t4, 8($t1) # save GPA. . . . • . . . . . . GPA hours fees . . . +8 +4 +0 base offset Florida A & M University - Department of Computer and Information Sciences
... 24 A[6] 20 … 16 … 12 A[3] 8 A[2] 4 A[1] 0 A[0] Addressing Array Elements • An array is a collection of variables whose values are stored in consecutive memory cells. int A[6]; // each element takes 1 word (4 bytes) char C[15]; // each element takes 1 byte • offset = 0 for first element in array (A[0], C[0]) • Examples: • A[3] offset is the #bytes occupied by A[0], A[1] and A[2] = 3 x 4 bytes = 12 • C[3] offset is the #bytes occupied by C[0], C[1] and C[2] = 3 x 1 byte = 3 • offset is the relative location of data within a collection. Florida A & M University - Department of Computer and Information Sciences
Array Arithmetic Example int m, d, F[20]; . . . m = d + F[10]; • . . . • la $t3, F # Establish base register. • lw $s1, d # value of d • lw $s2, 40($t3) # value of F[10] • add $s3, $s1, $s2 # d + F[10] • sw $s3, m # store result in m. • . . . • .data • F: .space 80 # 4 bytes x 20 elements • m: .word • d: .word Florida A & M University - Department of Computer and Information Sciences
Array Arithmetic – Your Turn • How do you do the following C++ statements? int p, d, F[20]; . . . F[14] = p – d; Florida A & M University - Department of Computer and Information Sciences
Registers vs. Memory • Register access is faster than memory • Operating on memory data requires loads and stores – slower than registers • Compiler must use registers to achieve performance • Spill to memory those variables used the least. • Register optimization is important! Florida A & M University - Department of Computer and Information Sciences
MIPS Registers – Usage Chart • 32 registers • 16 used for general purposes • $0 special constant value (=0) • Others – http://chortle.ccsu.edu/AssemblyTutorial/zAppendixB/registerUseChart.html Florida A & M University - Department of Computer and Information Sciences
Constants • Small constants are frequent (50%) operands • v = v + 5; • Approaches ( v = v + 5 ) • Allocate memory cells for often used constants Five: .word 5 lw $s1, v lw $s2, Five add $s3, $s2, $s1 sw $s3, v • Use immediate value instructions lw $s1, v addi $s2, $s1, 5 sw $s2, v Florida A & M University - Department of Computer and Information Sciences
$zero Register • MIPS register $0 ($zero) is the constant 0 • $zero cannot be overwritten • add $zero,$zero,$s0will have no effect. • Register-Register pseudo-instruction move $t1, t2 Has the effect: addi $t2, $t1, $zero # add 0 + $t1 and store in $t2 Florida A & M University - Department of Computer and Information Sciences