1 / 16

CS/COE0447 Computer Organization & Assembly Language

CS/COE0447 Computer Organization & Assembly Language. Chapter 2 Part 4. Topics. Creating executables Assembler, linker, loader Compilers versus interpreters What does the assembler do for you? Review of branch and jump instructions. void swap(int v[], int k) { int temp; temp = v[k];

kenna
Download Presentation

CS/COE0447 Computer Organization & Assembly Language

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. CS/COE0447Computer Organization & Assembly Language Chapter 2 Part 4

  2. Topics • Creating executables • Assembler, linker, loader • Compilers versus interpreters • What does the assembler do for you? • Review of branch and jump instructions

  3. void swap(int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 00000000101000010… 00000000000110000… 10001100011000100… 10001100111100100… 10101100111100100… 10101100011000100… 00000011111000000… assembler compiler “C Program” Down to “Numbers”

  4. To Produce an Executable source file .asm/.s object file .obj/.o assembler source file .asm/.s object file .obj/.o executable .exe assembler linker source file .asm/.s object file .obj/.o library .lib/.a assembler

  5. Linker

  6. An Object File • Header • Size and position of other pieces of the file • Text segment • Machine codes • Data segment • Binary representation of the data in the source • Relocation information • Identifies instructions and data words that depend on absolute addresses • Symbol table • Keeps addresses of global labels • Lists unresolved references • Debugging information • Contains a concise description of the way in which the program was compiled

  7. An Assembler • Expands macros • Macro is a sequence of operations conveniently defined by a user • A single macro can expand to many instructions • Determines addresses and translates source into binary numbers • Record in “symbol table” addresses of labels • Resolve branch targets and complete branch instructions’ encoding • Record instructions that need be fixed after linkage • Packs everything in an object file • “Two-pass assembler” • To handle forward references • bne $t0,$t1,next_label • next_label:

  8. Assembler Directives • Guides the assembler to properly handle following codes with certain considerations • .text • Tells assembler that codes follow • .data • Tells assembler that data follow • .align • Directs aligning the following items • .global • Tells to treat the following symbol as global • .asciiz • Tells to handle the following as a “string”

  9. .data int_str: .asciiz “%d” .text .macro print_int($arg) la $a0, int_str mov $a1, $arg jal printf .end_macro … print_int($7) la $a0, int_str mov $a1, $7 jal printf Macro Example

  10. Branch and Jump Instructions Review

  11. Instruction Format • beq, bne PC = PC + 4 + BranchAddr • BranchAddr = {14{immediate[15]},immediate,2'b0} • Two issues: • Assembly  machine code • Execution of the machine code I op rs rt 16-bit immediate

  12. 0x00400024 0x00400028 0x0040002c 0x00400030 bne $t0,$s5,exitloop addi $s3,$s3,1 j loop exitloop: add $s6,$s3,$zero BNE machine code in binary: 000101 01000 10101 0000000000000010 BNE machine code in hex: 15150002 BranchAddr = {14{immediate[15]},immediate,2'b0} = {00000000000000,0000000000000010,00} = 0000 0000 0000 00 00 0000 0000 0000 1000 = 0x00000008 When BNE instruction is executed (condition true): Next address = PC + 4 + BranchAddr Next address = 00400024 + 4 + 00000008 = 00400030… address of the exitloop inst!

  13. BranchAddr: Why 2’b0 at the end? • BranchAddr might have been: • {number,00} = number * 4 (like shifting left by 2) • Recall: the immediate field of the instruction contains the number of instructions away the label is • Each instruction is 4 bytes, so multiplying by 4 gives you the number of bytes away it is. This is the number to add to PC + 4 to jump to the label. {16{immediate[15]},immediate}

  14. BranchAddress: Why 2’b0 at the end? • If immediate instead were the number of bytes away the label is, then we would be wasting 2 bits • Since all instruction addresses are multiples of 4, the bottom 2 bits are always 00 • By not including those bits in the immediate field of the machine code, branch instructions can be used to jump 4 times further away

  15. Instruction Format, cont’d • The address of next instruction is obtained by concatenating with PC PC = {PC[31:28],address,2’b0} Jump op 26-bit address

  16. 0x00400018 bne $s4, $s5, ELSE 0x0040001c add $s3, $s2, $s5 0x00400020 j EXIT ELSE: 0x00400024 sub $s3, $s2, $s5 0x00400028 addi $s5, $s5, 1 0x0040002c EXIT: addi $s4,$s4,1 j instruction machine code: Hex: 0810000b Look at execution: • PC = {PC[31:28],address,00} • PC[31:28] = 0000 • address = 00 0001 0000 0000 0000 0000 1011 • {0000, address, 00} = • 0000 00 0001 0000 0000 0000 0000 1011 00 BIN • 0 0 4 0 0 0 2 c HEX • The address EXIT stands for!

More Related