1 cs/coe0447 computer organization & assembly language chapter 2 part 3

22
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

Upload: myrtle-montgomery

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

3 Register Usage Convention NAMEREG. NUMBERUSAGE $zero0zero $at1reserved for assembler usage $v0~$v12~3values for results and expression eval. $a0~$a34~7arguments $t0~$t78~15temporaries $s0~$s716~23temporaries, saved $t8~$t924~25more temporaries $k0~$k126~27reserved for OS kernel $gp28global pointer $sp29stack pointer $fp30frame pointer $ra31return address

TRANSCRIPT

Page 1: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

1

CS/COE0447Computer Organization & Assembly

Language

Chapter 2 Part 3

Page 2: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

2

Topics

• Register Usage Conventions• Stack and Frame Pointers• Procedure Calls• Creating executables

– Assembler, linker, loader– Compilers versus interpreters

• What does the assembler do for you?

Page 3: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

3

Register Usage Convention

NAME REG. NUMBER USAGE

$zero 0 zero

$at 1 reserved for assembler usage

$v0~$v1 2~3 values for results and expression eval.

$a0~$a3 4~7 arguments

$t0~$t7 8~15 temporaries

$s0~$s7 16~23 temporaries, saved

$t8~$t9 24~25 more temporaries

$k0~$k1 26~27 reserved for OS kernel

$gp 28 global pointer

$sp 29 stack pointer

$fp 30 frame pointer

$ra 31 return address

Page 4: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

4

Stack and Frame Pointers

• Stack pointer ($sp)– Keeps the address to the top of the stack– $29 is reserved for this purpose– Stack grows from high address to low– Typical stack operations are push/pop

• Procedure frame– Contains saved registers and local variable

s– “Activation record”

• Frame pointer ($fp)– Points to the first word of a frame– Offers a stable reference pointer– $30 is reserved for this– Some compilers don’t use $fp

Page 5: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

5

Procedure Calls

• Argument passing– First 4 arguments are passed through $a0~$a3– More arguments are passed through stack

• Result passing– First 2 results are passed through $v0~$v1– More results can be passed through stack

• Stack manipulations can be tricky and error-prone

• A programming assignment will involve the stack

Page 6: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

6

“C Program” Down to “Numbers”

swap:muli $2, $5, 4add $2, $4, $2lw $15, 0($2)lw $16, 4($2)sw $16, 0($2)sw $15, 4($2)jr $31

void swap(int v[], int k){

int temp;temp = v[k];v[k] = v[k+1];v[k+1] = temp;

}

00000000101000010…00000000000110000…10001100011000100…10001100111100100…10101100111100100…10101100011000100…00000011111000000…

compiler

assembler

Page 7: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

7

To Produce an Executable

source file.asm/.s

source file.asm/.s

source file.asm/.s

object file.obj/.o

object file.obj/.o

object file.obj/.o

library.lib/.a

executable.exe

assembler

assembler

assembler

linker

Page 8: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

8

Linker

Page 9: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

9

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

Page 10: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

10

Interpreter vs. Compiler

Interpreter Compiler

Concept Line-by-line translation and execution Whole program translation and native execution

Example Java, BASIC, … C, Fortran, …

+Interactive

Portable (e.g., Java Virtual Machine)High performance

– Low performance Binary not portable to other machines or generations

Page 11: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

11

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

Page 12: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

12

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”

Page 13: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

13

Macro Example

.dataint_str:

.asciiz “%d”

.text.macro print_int($arg)

la $a0, int_strmov $a1, $argjal printf.end_macro

…print_int($7)

la $a0, int_strmov $a1, $7jal printf

To use macros in MARS, need to run it from the command line

Page 14: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

14

High-Level Lang. vs. Assembly

High-level language Assembly

Example C, Fortran, Java, … -

+High productivity

– Short description & readabilityPortability

Low productivity– Long description & low readability

Not portable

– Limited optimization capability in certain cases

With proper knowledge and experiences, fully optimized codes can be written

Page 15: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

15

Branch and Jump InstructionsReview

Page 16: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

16

Instruction Format

• beq, bne PC = PC + 4 + BranchAddr• Without delayed branching (in MARS!!)

– PC = PC + BranchAddr

• BranchAddr = {14{immediate[15]},immediate,2'b0}• Two issues:

– Assembly machine code– Execution of the machine code

16-bit immediatertrsopI

Page 17: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

17

bne $t0,$s5,exitloopaddi $s3,$s3,1 j loopexitloop: add $s6,$s3,$zero

0x004000240x004000280x0040002c0x00400030

BNE machine code in binary: 000101 01000 10101 0000000000000011BNE machine code in hex: 15150003

When BNE instruction is executed (condition true):Next address = PC + BranchAddrNext address = 00400024 + 0000000c = 00400030 … address of the exitloop inst!

BranchAddr = {14{immediate[15]},immediate,2'b0} = {00000000000000,0000000000000011,00} = 0000 0000 0000 00 00 0000 0000 0000 1100 = 0x0000000c

Page 18: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

18

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 the PC to jump to the label.

{16{immediate[15]},immediate}

Page 19: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

19

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

Page 20: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

20

Nice question for an Exam

• What would change in the previous example, if delayed branching were used in MARS?

• The immediate value would be 1 less• With delayed branching:

– PC = PC + 4 + BranchAddress– The “4” is due to the fact that the PC has already been incremented to

point to the next instruction

• In EG (slide 17): imm would be 0000000000000010• PC = PC + 4 + BranchAddress• BranchAddress = {14{0},0000000000000010,00} =

0x00000008• PC = 0x00400024 + 4 + 8 = 0x00400030

Page 21: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

21

Instruction Format, cont’d

• The address of next instruction is obtained by concatenating with PC

PC = {PC[31:28],address,2’b0}

26-bit addressopJump

Page 22: 1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3

22

0x00400018 bne $s4, $s5, ELSE0x0040001c add $s3, $s2, $s50x00400020 j EXIT ELSE: 0x00400024 sub $s3, $s2, $s5 0x00400028 addi $s5, $s5, 10x0040002c EXIT: addi $s4,$s4,1

j instruction machine code:Hex: 0810000b Look at execution:

PC = {PC[31:28],address,00}

PC[31:28] = 0000address = 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!