assemblers

16
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Assemblers See: P&H Appendix B.1-2

Upload: abba

Post on 07-Jan-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Assemblers. See: P&H Appendix B.1-2. Examples. ... T: ADDI r4, r0, -1 BEQ r3, r0, B ADDI r4, r4, 1 LW r3, 0(r3) J T NOP B:. ... JAL L nop nop L:LW r5, 0(r31) ADDI r5, r5, 1 SW r5, 0(r31) . cs3410 Recap. C. int x = 10; x = 2 * x + 15;. compiler. MIPS - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Assemblers

Kevin WalshCS 3410, Spring 2010

Computer ScienceCornell University

Assemblers

See: P&H Appendix B.1-2

Page 2: Assemblers

2

Examples

...T: ADDI r4, r0,

-1BEQ r3, r0, BADDI r4, r4, 1LW r3, 0(r3)J TNOP

B: ...

...JAL Lnopnop

L: LW r5, 0(r31)ADDI r5, r5, 1SW r5, 0(r31)...

Page 3: Assemblers

3

cs3410 Recap

3

int x = 10;x = 2 * x + 15;

Ccompiler

addi r5, r0, 10muli r5, r5, 2addi r5, r5, 15

MIPSassembly

001000000000010100000000000010100000000000000101001010000100000000100000101001010000000000001111

machinecode

assembler

CPU

Circuits

Gates

Transistors

Silicon

Page 4: Assemblers

4

Example 1

...T: ADDI r4,r0,-1BEQ r3, r0, BADDI r4,r4, 1LW r3, 0(r3)J TNOP

B: ...

...001000

000100

001000

100011

000010

00000000000000000000000000000000

...

Page 5: Assemblers

5

References

Q: How to resolve labels into offsets and addresses?

A: Two-pass assembly• 1st pass: lay out instructions and data, and build

a symbol table (mapping labels to addresses) as you go• 2nd pass: encode instructions and data in binary, using

symbol table to resolve references

Page 6: Assemblers

6

Example 2

...JAL Lnopnop

L:LW r5, 0(r31)ADDI r5,r5,1SW r5, 0(r31)...

...00100000000100000000000000000100

00000000000000000000000000000000

00000000000000000000000000000000

10001111111001010000000000000000

00001000101001010000000000000001

00000000000000000000000000000000

...

Page 7: Assemblers

7

Example 2 (better)

.text 0x00400000 # code segment...ORI r4, r0, counterLW r5, 0(r4)ADDI r5, r5, 1SW r5, 0(r4)...

.data 0x10000000 # data segmentcounter:

.word 0

Page 8: Assemblers

8

Lessons

Lessons:• Mixed data and instructions (von Neumann)• … but best kept in separate segments• Specify layout and data using assembler directives • Use pseudo-instructions

Page 9: Assemblers

9

Pseudo-Instructions

Pseudo-InstructionsNOP # do nothingMOVE reg, reg # copy between regsLI reg, imm # load immediate (up to 32 bits)LA reg, label # load address (32 bits)B label # unconditional branchBLT reg, reg, label # branch less than

Page 10: Assemblers

10

Assembler

Assembler:assembly instructions+ psuedo-instructions+ data and layout directives= executable program

Slightly higher level than plain assemblye.g: takes care of delay slots

(will reorder instructions or insert nops)

Page 11: Assemblers

11

Motivation

Q: Will I program in assembly?A: I do...• For kernel hacking, device drivers, GPU, etc.• For performance (but compilers are getting better)• For highly time critical sections• For hardware without high level languages• For new & advanced instructions: rdtsc, debug

registers, performance counters, synchronization, ...

Page 12: Assemblers

12

Stages

calc.c

math.c

io.s

libc.o

libm.o

calc.s

math.s

io.o

calc.o

math.ocalc.exe

Page 13: Assemblers

13

Anatomy of an executing program

0xfffffffc

0x00000000

top

bottom

0x7ffffffc0x80000000

0x10000000

0x00400000

Page 14: Assemblers

14

Example program

vector v = malloc(8);v->x = prompt(“enter x”);v->y = prompt(“enter y”);int c = pi + tnorm(v);print(“result”, c);

calc.c

int tnorm(vector v) { return abs(v->x)+abs(v->y);}

math.c

global variable: pientry point: promptentry point: printentry point: malloc

lib3410.o

Page 15: Assemblers

15

math.s

int abs(x) {return x < 0 ? –x : x;

}int tnorm(vector v) { return abs(v->x)+abs(v->y);}

math.c

tnorm:# arg in r4, return address in r31# leaves result in r4

abs:# arg in r3, return address in r31# leaves result in r3

Page 16: Assemblers

16

calc.s

vector v = malloc(8);v->x = prompt(“enter x”);v->y = prompt(“enter y”);int c = pi + tnorm(v);print(“result”, c);

calc.c dostuff:# no args, no return value, return addr in r31MOVE r30, r31LI r3, 8 # call malloc: arg in r3, ret in r3JAL mallocMOVE r6, r3 # r6 holds vLA r3, str1 # call prompt: arg in r3, ret in r3JAL promptSW r3, 0(r6)LA r3, str2 # call prompt: arg in r3, ret in r3JAL promptSW r3, 4(r6)MOVE r4, r6 # call tnorm: arg in r4, ret in r4JAL tnorm LA r5, piLW r5, 0(r5)ADD r5, r4, r5 LA r3, str3 # call print: args in r3 and r4MOVE r4, r5JAL printJR r30

.datastr1: .asciiz “enter x”str2: .asciiz “enter y”str3: .asciiz “result”.text

.extern prompt

.extern print

.extern malloc

.extern tnorm

.global dostuff