1 / 82

ECE243

ECE243. The NIOS ISA. The NIOS II ISA. Memory : 32-bit address space an address is 32bits Byte-addressable each address represents one byte Hence: 2 32 addresses = 2 32 bytes = 4GB Note: means NIOS capable of addressing 4GB doesn’t mean DE2 has that much memory in it

lovie
Download Presentation

ECE243

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. ECE243 The NIOS ISA

  2. The NIOS II ISA • Memory: • 32-bit address space • an address is 32bits • Byte-addressable • each address represents one byte • Hence: 232 addresses = 232bytes = 4GB • Note: means NIOS capable of addressing 4GB • doesn’t mean DE2 has that much memory in it • INSTRUCTION DATATYPES • defined by the ISA (in C: unsigned, char, long etc) • byte (b) = 8 bits • Half-word (h) = 2 bytes = 16bits • word (w) = 4 bytes = 32 bits

  3. ALIGNMENT • Processor expects: • half-words and words to be properly aligned • Half-word: address evenly divisible by 2 • Word: address evenly divisible by 4 • Byte: address can be anything • Why? Makes processor internals more simple • Ex: Load word at 0x27 Load halfword at 0x32 Load word at 0x6 Load word at 0x14 Load byte at 0x5

  4. REGISTERS CPU Memory (RAM) BUS • An array of flipflops • managed as a unit • Holds bits: • Can be interpreted as data, address, instr • Are internal to the CPU • much faster than memory • The PC is a register too • address of an instruction in memory Regs

  5. NIOS Registers • 32bits each • 32 general purpose registers: called r0-r31 • 6 control registers (learn more later) • 1 PC: called pc • GENERAL PURPOSE REGISTERS • r0: hardwired to always be zero 0x00000000 • r8-r23: for your use • r1-r7, r24-r31: reserved for specific uses

  6. Common Operations • Math: • add r8,r9,r10 • sub r8,r9,r10 • Also: mul, div • Logical: • or r8,r9,r10 • Example: 1010 | 0110 = • Also: and, nor, xor • Copying: • mov r8,r9

  7. EXAMPLE PROGRAM 1 • C-code: • Unsigned char a = 0x0; # unsigned char==1byte • Unsigned char b = 0x1; • Unsigned char c = 0x2; • a = b + c; • c = b • Assume already init: r8=a, r9=b, r10=c:

  8. How to Initialize a Register • movi instruction: • “move immediate” • movi r8,Imm16 # r8 = Imm16 • Imm16: • a 16-bit constant • called an “immediate operand” • can be decimal, hex, binary • positive or negative

  9. EXAMPLE PROGRAM 1 • C-code: • Unsigned char a = 0x0; # unsigned char==1byte • Unsigned char b = 0x1; • Unsigned char c = 0x2; • a = b + c; • c = b • With Initialization:

  10. EXAMPLE PROGRAM 2 • Assume unsigned int == 4 bytes == 1 word • unsigned int a = 0x00000000; • unsigned int b = 0x11223344; • unsigned int c = 0x55667788; • a = b + c; • With initialization:

  11. movia Instruction • “move immediate address” • movia r9, Imm32 # r9 = Imm32 • Imm32 • a 32-bit unsigned value (or label, more later) • doesn’t actually have to be an address!

  12. EXAMPLE PROGRAM 2 • Assume unsigned int == 4 bytes == 1 word • unsigned int a = 0x00000000; • unsigned int b = 0x11223344; • unsigned int c = 0x55667788; • a = b + c; • Solution:

  13. MEMORY INSTRUCTIONS • Used to copy to/from memory/registers • Load: ldw rX, Imm16(rY) • Performs: Rx = mem[rY + Imm16] • rY: holds the address to be accessed in memory • Imm16: • sometimes called a ‘displacement’ • can be positive or negative • is actually a 2’s complement number • Is added to the address in rY (but doesn’t change rY) • Store: stw rX, Imm16(rY) • performs: mem[rY + Imm16] = Rx

  14. Types of Loads and Stores • Load granularities: • ldw: load word; ldh: load halfword; ldb: load byte • Store granularities: • stw: store word; sth: store halfword; stb: store byte • load and store instructions that end in ‘io’ • eg: ldwio, stwio • this means to bypass the cache (later) if one exists • important for memory-mapped addresses (later) • otherwise same as ldw and stw

  15. Example Program 3 • unsigned int a = 0x00000000; • unsigned int b = 0x11223344; • unsigned int c = 0x55667788; • a = b + c; • Challenge: • keep a,b,c in memory instead of registers • Assume memory is already initialized:  • Addr: Value • 0x200000: 0x00000000 • 0x200004: 0x11223344 • 0x200008: 0x55667788

  16. Example Program 3 • unsigned int a = 0x00000000; • unsigned int b = 0x11223344; • unsigned int c = 0x55667788; • a = b + c; • Solution: • Memory: • Addr: Value • 0x200000: 0x00000000 • 0x200004: 0x11223344 • 0x200008: 0x55667788 • .

  17. Optimized Program 3 • unsigned int a = 0x00000000; • unsigned int b = 0x11223344; • unsigned int c = 0x55667788; • a = b + c; • Solution: • Memory: • Addr: Value • 0x200000: 0x00000000 • 0x200004: 0x11223344 • 0x200008: 0x55667788 • .

  18. Addressing Modes • How you can describe operands in instrs • Addressing Modes in NIOS: • register: • immediate: • register indirect with displacement • register indirect: • Note: • other more complex ISAs can have many addressing modes (more later)

  19. ECE243 Assembly Basics

  20. Typical GNU/Unix Compilation gcc a.out foo.c ld cpp as cc1 foo.s foo.o foo.i a.out foo.c compiler assembly (text) pre-processor #defines etc. pre-proc’d C-Code (text) assembler linker object (binary) C-code (text) executable (binary) • assembly: • text (human readable) • linker: • can join multiple object files (and link-in libraries) • places instrs and data in specific memory locations

  21. Compilation in ECE243 foo.o nios2-elf-objcopy nios2-elf-ld foo.s foo.elf nios2-elf-as foo.srec linker Ascii encoding (binary) executable (binary) assembler assembly (text) object (binary) Binary translator download to de1/2 • in 243 you will mainly write assembly

  22. WHAT IS NOT IN AN ASSEMBLY LANGUAGE? • classes, hidden variables, private vs public • datatype checking • data structures: • arrays, structs • control structures: • loops, if-then-else, case stmts • YOU’RE ON YOUR OWN!

  23. Assembly File: 2 Main parts: • text section • declares procedures and insts • data section • reserves space for data • can also initialize data locations • data locations can have labels that you can refer to Example Assembly file: .section .data … .section .text …

  24. Ex: Assembly for this C-Code: unsigned int a = 0x00000000; unsigned int b = 0x11223344; unsigned int c = 0x55667788;  a = b + c; Ex: Data Section .section .data .align 2 va: .word 0 vb: .word 0x11223344 vc: .word 0x55667788 means “the following must be aligned to 22 =4 byte boundaries size (.word=.long=word, .hword=halfword, .byte=byte) Labels:

  25. Ex: When Loaded into Memory • linker decides where in mem to place data section • lets assume it places it starting at 0x200000 • .section and .align are ‘directives’ • they tell assembler to do something • but they don’t become actual instructions! • the labels and .long are also gone • only the data values are now in memory .section .data .align 2 va: .word 0 vb: .word 0x11223344 vc: .word 0x55667788 MEM: 0x200000: 0x00000000 0x200004: 0x11223344 0x200008: 0x55667788

  26. Ex: Assembly for this C-Code: unsigned int a = 0x00000000; unsigned int b = 0x11223344; unsigned int c = 0x55667788;  a = b + c; Ex: Text Section .section .text .global main main: movia r11, va ldw r9, 4(r11) ldw r10,8(r11) add r8, r9, r10 stw r8, 0(r11) ret means ‘main’ is visible to other files we can use the label va as a 32bit immediate value ‘main’ is special, it is always where execution starts we return, because main is a procedure call

  27. USEFUL ASSEMBLER DIRECTIVES AND MACROS • /* comments in here */ • # this comments-out the rest of the line • .equ LABEL, value • replace LABEL with value wherever it appears • remember: this does not become an instruction • .asci “mystring” # declares ascii characters • .asciz “mystring” # ends with NULL (0x0)  

  28. Arrays and Space • Myarray: .word 0, 1, 2, 3, 4, 5, 6, 7 • declares an array of 8 words • starts at label ‘Myarray’ • initializes to 0,1,2,3,4,5,6,7  • myspace: .skip SIZE • reserves SIZE bytes of space • starts at label ‘myspace’ • does not initialize (eg., does not set to zero) • myspace: .space SIZE • same as .skip but initializes all locations to 0

  29. Ex: Arrays and Space • Create an array of 4 bytes at the label myarray0 initialized to 0xF,0xA,0xC,0xE • Reserve space for an 8-element array of halfwords at the label myarray1, uninitialized • Reserve space for a 6-element array of words at the label myarray2, initialized to zero

  30. Understanding Disassembly • can run “make SRCS=foo.s disasm” • will dump a “disassembly” file of program • shows bare real instructions • all assembler directives are gone • Example disasm output: 01000000 <main>: 1000000: 02c04034 movi r11,256 1000004: 5ac01804 addi r11,r11,96 address of instruction in memory ie., PC value when it is executing is in hex (should be 0x…) binary encoding of instruction (later) is also in hex (should be 0x…)

  31. ECE243 Control Flow

  32. HOW TO IMPLEMENT THIS?: for (i=0;i<10;i++){ … } • NEED: • a way to test when a condition is met • a way to modify the PC • ie., not just execute the next instruction in order • ie., do something other than PC = PC + 4

  33. UNCONDITIONAL BRANCHES • Can change the PC • Starts execution at specified address • Unconditional branch: br br LABEL • does: PC = LABEL • unconditional: always! • Jump: jmp jmp rA • does: PC = rA • is also unconditional Example: MYLABEL: add r8,r9,r10 add r9,r8,r8 br MYLABEL Example: MYLABEL: add r8,r9,r10 add r9,r8,r8 movia r11,MYLABEL jmp r11

  34. Conditional branches • only branch if a certain condition is true bCC rA, rB, LABEL # if rA CC rB goto LABEL • does signed comparisons, ie. you can use negative numbers • CC: • eq (=), gt (>), ge (>=), lt (<), le (<=), ne (!=) Example: MYLABEL: add r8,r9,r10 add r9,r8,r8 bgt r8,r9,MYLABEL

  35. Ex: make these loops (assume r8 initialized) decrement r8 by 1, loop-back if r8 is non-zero, increment r8 by 1, loop-back if r8 is equal to r9 increment r8 by 4, loop-back if r8 greater than r9 decrement r8 by 2, loop-back if r8 less-than-eq-to zero

  36. Example If (r8 > r9){ … // THEN } else { … // ELSE }

  37. Example If (r8 == 5){ … // THEN } else { … // ELSE }

  38. Example If (r8 > r9 && r8 == 5){ … // THEN } else { … // ELSE }

  39. Example If (r8 <= r9 || r8 != 5){ … // THEN } else { … // ELSE }

  40. Example for (r8=1; r8 < 5; r8++){ r9= r9 + r10; }

  41. Example while (r8 > 8){ r9= r9 + r10; r8--; }

  42. ECE243 Stacks

  43. STACKS • LIFO data structure (last in first out) • Push: put new element on ‘top’ • Pop: take element off the ‘top’ • Example: push(5); push(8); pop();

  44. A STACK IN MEMORY • pick an address for the “bottom” of stack • stacks usually grow “upwards” • i.e., towards lower-numbered address locations • programs usually have a “program stack” • for use by user program to store things • NIOS: • register r27 is usually the “stack pointer” aka ‘sp’ • you can use ‘sp’ in your assembly programs • to refer to r27 • the system initializes sp to be 0x17fff80

  45. Ex: Stack in Memory of Halfwords • Example: push(5); push(8); pop();

  46. ASSEMBLY For Stack of Halfwords • initialize stack pointer to 0x2000 (bottom of stack) • Push: assume we want to push halfword in r8 • Pop: assume we want to pop halfword into r8 • Note: we grow then push, pop then shrink • by convention • could it be the other way? • Note2: you don’t actually delete the value • it is still there!

  47. ECE243 Subroutines

  48. SUBROUTINES void bar(){ return; } void foo(){ bar(); } • foo calls bar, bar returns to foo

  49. SUBR CALLS VS BRANCHES • a branch replaces the value of the PC • with the new location to start executing • so does a subroutine call • a subr call “remembers where it came from” • how?

  50. RETURN ADDRESS REGISTER • r31 is the return address register (aka ra) • by NIOS convention • you can use ‘ra’ in assembly • NIOS convention for managing return addr: • push the previous return address (ra) on the stack • save the most recent return address in ra

More Related