directives and control flow
play

Directives and Control Flow 2 Directives Pseudo-instructions - PowerPoint PPT Presentation

1 EE 109 Unit 10 Assembler Directives and Control Flow 2 Directives Pseudo-instructions ASSEMBLERS 3 Writing Assembly Code written at the assembly level needs some additional help for specifying certain things Global variables


  1. 1 EE 109 Unit 10 – Assembler Directives and Control Flow

  2. 2 Directives Pseudo-instructions ASSEMBLERS

  3. 3 Writing Assembly • Code written at the assembly level needs some additional help for specifying certain things – Global variables – Where code and data should be placed in memory – Easy ways to reference memory locations • To help us do this assemblers provide some additional statements that we can use

  4. 4 Our Simulator - MARS Download at: http://courses.missouristate.edu/KenVollmar/MARS/

  5. 5 Assembler Syntax • In MARS and most assemblers each line of the assembly program may be one of three possible options – Comment – Instruction / Pseudo-instruction – Assembler Directive

  6. 6 Comments • In MARS an entire line can be marked as a comment by starting it with a pound (#) character: • Example: # This line will be ignored by the assembler LW $2,8($3) ADDI $2,$2,1 ...

  7. 7 Instructions • In MARS each instruction is written on a separate line and has the following syntax: (Label:) Instruc. Op. Operands Comment • Example: START: ADD $2,$3,$4 # R[2]=R[3] + R[4] • Notes: – Label is optional and is a text identifier for the address where the instruction is placed in memory. (These are normally used to identify the target of a branch or jump instruction.) – In MARS, a comment can be inserted after an instruction by using a ‘#’ sign – A label can be on a line by itself in which case it refers to the address of the first instruction listed after it

  8. 8 Labels and Instructions • The optional label in front of an instruction evaluates to the address where the instruction or data starts in memory and can be used in other instructions .text START: LW $4,8($10) LW 0x400000 = START L1: ADDI $4,$4,-1 BNE $4,$0,L1 ADDI 0x400004 = L1 J START BNE 0x400008 Assembly Source File J 0x40000C .text Note: The BNE instruc. causes the Assembler finds what LW $4,8($10) program to branch (jump) to the address each instruction ADDI $4,$4,-1 instruction at the specified address if the starts at… BNE $4,$0,0x400004 two operands are Not Equal. The J(ump) J 0x400000 instruction causes program execution to jump to the specified label (address). …and replaces the labels with their corresponding address

  9. 9 Assembler Directives • Similar to pre-processor statements (#include, #define, etc.) and global variable declarations in C/C++ – Text and data segments – Reserving & initializing global variables and constants – Compiler and linker status • Direct the assembler in how to assemble the actual instructions and how to initialize memory when the program is loaded

  10. 10 An Example x: • This is output from an .word 5 .globl nums .section .bss actual MIPS gcc/g++ .align 2 .type nums, @object compiler .size nums, 40 nums: .space 40 • Actual instructions are at .text .align 2 the bottom (addiu, srl, etc.) .globl _Z6calleei $LFB2: .ent _Z6calleei • Directives are the things _Z6calleei: .frame $sp,0,$31 starting with . .mask 0x00000000,0 .fmask 0x00000000,0 addiu $2,$4,3 • Labels are names ending srl $3,$2,31 addu $2,$2,$3 with : • Let's learn about some of the directives

  11. 11 Text and Static Data Segments • .text directive indicates the I/O Space 0xFFFF_FFFC following instructions should be placed in the program area of 0x8000_0000 memory Stack 0x7FFF_FFFC • .data directive indicates the following data declarations will Dynamic Data be placed in the data memory Segment segment Static Data Segment 0x1000_8000 0x1000_0000 Text Segment 0x0040_0000 Unused 0x0000_0000

  12. 12 Static Data Directives • Fills memory with specified data when program is loaded • Format: val_0,val_1,…, val_n ( Label :) . type_id • type_id = {.byte, .half, .word, .float, .double} • Each value in the comma separated list will be stored using the indicated size – Example: myval: .word 1, 2, 0x0003 • Each value 1, 2, 3 is stored as a word (i.e. 32-bits) • Label “myval” evaluates to the start address of the first word (i.e. of the value 1)

  13. 13 More Static Data Directives • Can be used to initialize ASCII strings • Format: “ string ” ( Label :) .ascii “ string ” ( Label :) .asciiz • .asciiz adds a null-termination character (0) at the end of the string while .ascii does not – Example: string: .asciiz “Hello world \ n” • Each character stored as a byte (including \n = Line Feed and \0 = Null (0) character) • Label “ myval ” evaluates to the start address of the first byte of the string

  14. 14 Reserving Memory • Reserves space in memory but leaves the contents unchanged • Format: ( Label :) .space num_bytes 00 00 00 00 0x1000000C .data dat1: .word 0x12345678 FE DC BA 98 0x10000008 = dat2 array: .space 4 dat2: .word 0xFEDCBA98 00 00 00 00 0x10000004 = array Skipped 12 34 56 78 0x10000000 = dat1

  15. 15 Alignment Directive • Used to skip to the next, correctly-aligned address for the given data size • Format: .align 0,1,2, or 3 • 0 = byte-, 1 = half-, 2 = word-, 3 = double-alignment .data dat1: .byte 1, 2, 3 00 00 00 00 0x1000000C .align 1 89 AB CD EF 0x10000008 = dat3 dat2: .half 0x4567 .align 2 Skipped 00 00 45 67 0x10000004 = dat2 dat3: .word 0x89ABCDEF 00 03 02 01 0x10000000 = dat1 Skipped Note: The number after .align is not how many bytes to skip, it indicates what type of data will come next and thus the size to be aligned

  16. 16 .data example Examples .data 12 34 56 78 0x1001000C C1: .byte 0xFE,0x05 00 00 00 02 0x10010008 MSG: .asciiz “SC \ n” Skipped because a DAT: .half 1,2 00 01 00 0A 0x10010004 word must begin on .align 2 a 4-byte boundary VAR: .word 0x12345678 43 53 05 FE 0x10010000 • C1 evaluates to 0x10001000 • MSG evaluates to 0x10001002 (Note: \n = Line Feed char. = 0x0A) • DAT evaluates to 0x10001006 • VAR evaluates to 0x1000100C

  17. 17 C/C++ and Directives • Directives are used to initialize or reserve space for global variables in C short int count = 7; .data char message[16]; count: .half 7 int table[8] = {0,1,2,3,4,5,6,7}; message: .space 16 .align 2 void main() table: .word 0,1,2,3,4,5,6,7 { ... .text } .globl main main: ... C/C++ style global declarations Assembly equivalent

  18. 18 Summary & Notes • Assembler Directives: – Tell the assembler how to build the program memory image • Where instructions and data should be placed in memory when the program is loaded • How to initialize certain global variables • Recall, a compiler/assembler simply outputs a memory IMAGE of the program. It must then be loaded into memory by the OS to be executed. • Key: Directives are NOT instructions! – They are used by the assembler to create the memory image and then removed – The MIPS processor never sees these directives!

  19. 19 Directives in the Software Flow Assembler Directives High Level are used to create the Language object code (executable) Description .data image… MOVE.W X,D0 int n = 0xC259; n: .word 0xC259 CMPI.W #0,D0 Compiler .text void main(){ BLE SKIP SLT $4,$2,$0 if (x > 0) ADD Y,D0 BNE SKIP Assembler x = x + y - z; SUB Z,D0 … SKIP MUL … a = b*x; SKIP: MUL … .c/.cpp files Assembly (.asm/.s files) 1110 0010 0101 1001 1110 0010 0101 1001 0110 1011 0000 1100 0110 1011 0000 1100 0100 1101 0111 1111 0100 1101 0111 1111 1010 1100 0010 1011 …the processor NEVER 1010 1100 0010 1011 0001 0110 0011 1000 0001 0110 0011 1000 sees/executes these directives Object/Machine Code PC (.o files) 1110 0010 0101 1001 0110 1011 0000 1100 Program 0100 1101 0111 1111 Executing 1010 1100 0010 1011 Loader / 0001 0110 0011 1000 Linker OS SLT BNE Executable Binary Image

  20. 20 Assembly Process • The assembly procedure of a file requires two passes over the code – 1 st Pass: Build a symbol table • Maps labels to corresponding addresses – 2 nd Pass: Substitute corresponding values for labels and symbols and then translate to machine code

  21. 21 Assembly Process Diagram Host System Target System .data 1 st Pass (PC) (MIPS) count: .word -1 .text MIPS Assembler (MARS) Main: la $2,count Proc. .s Assembly Input File Symbol Table count = 0x10010000 400000 PC MAIN = 0x400000 FF FF FF FF 0x10010000 2 nd Pass .data Data Structure … count: .word -1 (Part of Assembler .text 3C 02 10 01 0x00400000 Program) Main: la $2,0x10010000 @0x10010000:0xFFFFFFFF @0x400000:0x3c021001 “Executable” File

  22. 22 Pseudo-instructions • “Macros” translated by the assembler to instructions actually supported by the HW • Simplifies writing code in assembly • Example – LI (Load-immediate) pseudo- instruction translated by assembler to 2 instruction sequence (LUI & ORI) ... ... lui $2, 0x1234 li $2, 0x12345678 ori $2, $2, 0x5678 ... ... After assembler… With pseudo-instruction

  23. 23 Pseudo-instructions Pseudo-instruction Actual Assembly NOT Rd,Rs NOR Rd,Rs,$0 NEG Rd,Rs SUB Rd,$0,Rs LI Rt, immed. # Load Immediate LUI Rt, {immediate[31:16], 16’b0} ORI Rt , {16’b0, immediate[15:0]} LA Rt, label # Load Address LUI Rt, {immediate[31:16], 16’b0} ORI Rt , {16’b0, immediate[15:0]} BLT Rs,Rt,Label SLT $1,Rs,Rt BNE $1,$0,Label Note: Pseudoinstructions are assembler-dependent. See MARS Help for more details.

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