cpsc 213
play

CPSC 213 Read assembly code Java weak references, reference - PowerPoint PPT Presentation

Learning Goals 1 Learning Goals 2 Not Covered on Final Memory Read Assembly Details of memory management Endianness and memory-address alignment CPSC 213 Read assembly code Java weak references, reference objects,


  1. Learning Goals 1 Learning Goals 2 Not Covered on Final ‣ Memory ‣ Read Assembly ‣ Details of memory management • Endianness and memory-address alignment CPSC 213 • Read assembly code •Java weak references, reference objects, reference queues ‣ Globals ‣ Write Assembly - slides 22-24 of module 1c, details of Lab 3 Java memory leak solution • Machine model for access to global variables; static and dynamic arrays and structs • Write assembly code •C reference counting ‣ Pointers ‣ ISA-PL Connection - slides 17-18 of module 1c • Pointers in C, & and * operators, and pointer arithmetic ‣ Details of Hoare blocking signal for condition variables • Connection between ISA and high-level programming language ‣ Instance Variables ‣ Asynchrony •slides 24-26 of module 2c • Instance variables of objects and structs Introduction to Computer Systems ‣ Dynamic Storage • PIO, DMA, interrupts and asynchronous programming ‣ OS/Encapsulation ‣ Threads • Dynamic storage allocation and deallocation •module 2e ‣ If and Loop • Using and implementing threads ‣ Interprocess Communication, Networking, Protocols Unit 3 • If statements and loops ‣ Synchronization ‣ Procedures •module 2f Course Review • Using and implementing spinlocks, monitors, condition variables and semaphores • Procedures, call, return, stacks, local variables and arguments ‣ Virtual Memory ‣ Dynamic Flow Control • Virtual memory translation and implementation tradeoffs • Dynamic flow control, polymorphism, and switch statements 1 2 3 4 Big Ideas: First Half Memory Access Loading and Storing Numbers dec hex bin 0 0 0000 ‣ Static and dynamic ‣ Hex vs. decimal vs. binary ‣ Memory is ‣ load into register 1 1 0001 2 2 0010 • immediate value: 32-bit number directly inside instruction • an array of bytes, indexed by byte address •anything that can be determined before execution (by compiler) is called •in SM-213 assembly 3 3 0011 • from memory: base in register, direct offset as 4-bit number ‣ Memory access is static 4 4 0100 - offset/4 stored in machine language - 0x in front of number means it’s in hex - common mistake: forget 0 offset when just want store value from register into memory 5 5 0101 • restricted to a transfer between registers and memory •anything that can only be determined during execution (at runtime) is - otherwise it’s decimal • from memory: base in register, index in register 6 6 0110 • the ALU is thus unchanged, it still takes operands from registers called dynamic - computed offset is 4*index 7 7 0111 •converting from hex to decimal • from register • this is approach taken by Reduced Instruction Set Computers (RISC) 8 8 1000 ‣ SM-213 Instruction Set Architecture - convert each hex digit separately to decimal ‣ store into memory ‣ Common mistakes 9 9 1001 - 0x2a3 = 2x16 2 + 10x16 1 + 3x16 0 • base in register, direct offset as 4-bit number 10 A 1010 •hardware context is CPU and main memory with fetch/execute loop • wrong: trying to have instruction read from memory and do computation all at once • base in register, index in register 11 B 1011 •converting from hex to binary - must always load from memory into register as first step, then do ALU computations from registers only • common mistake: cannot directly store immediate value into memory valC 12 C 1100 - convert each hex digit separately to binary: 4 bits in one hex digit • wrong: trying to have instruction do computation and store into memory all at once 13 D 1101 dst Name Semantics Assembly Machine •converting from binary to hex 14 E 1110 - all ALU operations write to a register, then can store into memory on next step CPU Memory CPU srcA 15 F 1111 load immediate r[ d ] ← v ld $ v , r d 0d -- vvvvvvvv - convert each 4-bit block to hex digit 0: srcB load base+offset r[ d ] ← m[r[ s ]+(o=p*4)] ld o(r s ), r d 1psd 1: •exam advice ALU 2: r[ d ] ← m[r[ s ]+4*r[ i ]] opCode load indexed ld (r s ,r i ,4), r d 2sid 3: - reconstruct your own lookup table in the margin if you need to do this 4: register move r[ d ] ← r[ s ] mov rs, rd 60sd 5: Fetch Instruction from Memory Execute it Memory Tick Clock store base+offset m[r[ d ]+(o=p*4)] ← r[ s ] st r s , o(r d ) 3spd 6: 7: store indexed m[r[ d ]+4*r[ i ]] ← r[ s ] st r s , (r d ,r i ,4) 4sdi 5 6 7 8 Numbers Endianness Determining Endianness of a Computer Alignment Memory ‣ Common mistakes ‣ Consider 4-byte memory word and 32-bit register ‣ Power-of-two aligned addresses simplify hardware ... - treating hex number as decimal: interpret 0x20 as 20, but it’s actually decimal 32 i #include <stdio.h> •it has memory addresses i, i+1, i+2, and i+3 •required on many machines, faster on all machines - using decimal number instead of hex: writing 0x20 when you meant decimal 20 ✗ •we’ll just say its “ at address i and is 4 bytes long ” i + 1 int main () { ✗ ✗ char a[4]; •e.g., the word at address 4 is in bytes 4, 5, 6 and 7. i + 2 - wasting your time converting into format you don’t particularly need ‣ Big or Little Endian *((int*)a) = 1; - wasting your time trying to do computations in unhelpful format i + 3 •computing alignment: for what size integers is address X aligned? • think: what do you really need to answer the question? •we could start with the BIG END of the number printf("a[0]=%d a[1]=%d a[2]=%d a[3]=%d\n",a[0],a[1],a[2],a[3]); ... • adding small numbers easy in hex: B+2=D } - byte address to integer address is division by power to two, which is just shifting bits - most computer makers except for Intel, also network protocols • for serious computations consider converting to decimal j / 2 k == j >> k (j shifted k bits to right) • unless multiply/divide by power of 2: then hex or binary is fast with bitshifting! i i + 1 i + 2 i + 3 - convert address to decimal; divide by 2, 4, 8, 16, .....; stop as soon as there’s a remainder • how does this C code check for endianness? Register bits 2 31 to 2 24 2 23 to 2 16 2 15 to 2 8 2 7 to 2 0 - convert address to binary; sweep from right to left, stop when find a 1 - create array of 4 bytes (char data type is 1 byte) •or we could start with the LITTLE END - cast whole thing to an integer, set it to 1 - Intel - check if the 1 appears in first byte or last byte • things to understand: i + 3 i + 2 i + 1 i - concepts of endiananess Register bits 2 31 to 2 24 2 23 to 2 16 2 15 to 2 8 2 7 to 2 0 - casting between arrays of bytes and integers - masking bits, shifting bits 9 10 11 12 Static Variable Access (static arrays) Static vs Dynamic Arrays Dereferencing Registers Basic ALU Operations Static Memory Layout ‣ Same access, different declaration and allocation ‣ Common mistakes ‣ Arithmetic 0x1000: value of a int a; int b[10]; 0x2000: value of b[0] • no dereference when you need it •for static arrays, the compiler allocates the whole array Name Semantics Assembly Machine 0x2004: value of b[1] register move r[ d ] ← r[ s ] • extra dereference when you don’t need it mov rs, rd 60sd void foo () { •for dynamic arrays, the compiler allocates a pointer b[a] = a; ... .... add r[ d ] ← r[ d ] + r[ s ] add rs, rd 61sd • example b[a] = a; 0x2020: value of b[9] int a; int a; and r[ d ] ← r[ d ] & r[ s ] and rs, rd 62sd } int* b; int b[10]; inc r[ d ] ← r[ d ] + 1 inc rd 63 - d ld $a_data, r0 # r0 = address of a ld (r0), r1 # r1 = a inc address r[ d ] ← r[ d ] + 4 void foo () { inca rd 64 - d void foo () { ld $b_data, r2 # r2 = address of b b = (int*) malloc (10*sizeof(int)); ‣ Key observations b[a] = a; dec r[ d ] ← r[ d ] - 1 dec rd 65 - d ld (r2), r3 # r3 = b b[a] = a; } st r1, (r3,r1,4) # b[a] = a dec address r[ d ] ← r[ d ] - 4 deca rd 66 - d } •address of b[a] cannot be computed statically by compiler r[ d ] ← ~ r[ d ] not not rd 67 - d 0x2000: value of b[0] - a dereferenced once •address can be computed dynamically from base and index stored in 0x2000: value of b 0x2004: value of b[1] ‣ Shifting, NOP and Halt registers - b dereferenced twice ... • once with offset load ld $a_data, r0 # r0 = address of a - element size can known statically, from array type Name Semantics Assembly Machine 0x2024: value of b[9] ld (r0), r1 # r1 = a • once with indexed store ‣ Array access: use load/store indexed instruction shift left r[ d ] ← r[ d ] << S = s shl rd, s ld $b_data, r2 # r2 = address of b • no dereference: value in register 7d SS 7d SS ld (r2), r3 # r3 = b shift right r[ d ] ← r[ d ] << S = - s shr rd, s ld $a_data, r0 # r0 = address of a st r1, (r3,r1,4) # b[a] = a Name Semantics Assembly Machine • one dereference: address in register ld (r0), r1 # r1 = a halt halt machine halt f0 -- ld $b_data, r2 # r2 = address of b load indexed r[ d ] ← m[r[ s ]+4*r[ i ]] ld (r s ,r i ,4), r d 2sid • two dereferences: address of pointer in register nop do nothing nop fg -- st r1, (r2,r1,4) # b[a] = a extra dereference store indexed m[r[ d ]+4*r[ i ]] ← r[ s ] st r s , (r d ,r i ,4) 4sdi 13 14 15 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend