CSCI341 Lecture 21, MIPS Programming REVIEW Assemblers understand - - PowerPoint PPT Presentation

csci341
SMART_READER_LITE
LIVE PREVIEW

CSCI341 Lecture 21, MIPS Programming REVIEW Assemblers understand - - PowerPoint PPT Presentation

CSCI341 Lecture 21, MIPS Programming REVIEW Assemblers understand special commands called directives instructions for the assembler, eg, .text Assemblers understand macro commands higher-level instructions that abstract


slide-1
SLIDE 1

Lecture 21, MIPS Programming

CSCI341

slide-2
SLIDE 2

REVIEW

  • Assemblers understand special commands called “directives”
  • instructions for the assembler, eg, .text
  • Assemblers understand “macro” commands
  • higher-level instructions that abstract multiple “integer”

instructions, eg, mult vs mul

slide-3
SLIDE 3

ASSEMBLY LANGUAGE

One-to-one correspondence with machine instruction. Assembly is easier for humans to read than 010101, etc.

slide-4
SLIDE 4

ASSEMBLY LANGUAGE

mnemonics Symbols that help us “remember” something else. The mnemonic addi helps us “remember” 00...10..11.

slide-5
SLIDE 5

ASSEMBLY PROCESS

source assembler

  • bject file

source assembler

  • bject file

linker libs executable

slide-6
SLIDE 6

ASSEMBLY PROCESS

high-level source compiler

  • bject

file high-level source compiler

  • bject

file linker executable

assembler assembler

slide-7
SLIDE 7

ASSEMBLY = CONTROL (AND SPEED)

Often difficult to ensure high-level language program responds within definite time interval.

slide-8
SLIDE 8

PROFILING

Measuring where a program spends its time (among other things). Performance improvements may require recoding a critical part in assembly language. eg, Embedding assembly in parts of your C program.

slide-9
SLIDE 9

MAN VS. MACHINE

Historically, when compilers were more primitive, humans could often out-optimize the compiler.

slide-10
SLIDE 10

Today? Not so much.

slide-11
SLIDE 11

HOW DOES AN ASSEMBLER WORK?

  • Bind program labels to memory locations.
  • Translate assembly instructions to an object file.
slide-12
SLIDE 12

WHAT’S AN OBJECT FILE?

  • Contains machine instructions, data, and “bookkeeping”
  • Typically is never directly executed, because it references...
  • procedures or data in other files
slide-13
SLIDE 13

LOCAL VS. GLOBAL

  • Programs usually generated from multiple assembly files /
  • bject files.
  • Some labels need to remain “local” or “private” to a file.
  • Others need to be “global” and accessible from all files.
slide-14
SLIDE 14

CONSIDER...

.text .globl main main: subu $sp, $sp, 32 ... flower_loop: lw $t6, 28($sp) ... jal printf ... ...

That better be a global label, since it’s not declared in this file. local label global label “hey assembler, make sure all the object files you create know about this label.”

slide-15
SLIDE 15

.globl SomeSymbol

Declare SomeSymbol as global, so it can be referenced from other files.

slide-16
SLIDE 16

LOCAL SYMBOLS

If a label isn’t global, then it’s local. “A label is local if the label can be used only within the file in which it is defined.”

slide-17
SLIDE 17

THE LINKER

  • The assembler processes one file at a time, in isolation.
  • It only knows the addresses of local labels!
  • The linker resolves external labels.
  • But the assembler “helps,” by providing lists of labels and

unresolved references. (Part of the “bookkeeping”)

slide-18
SLIDE 18

HOW DOES AN ASSEMBLER WORK?

  • Bind program labels to memory locations.
  • Translate assembly instructions to an object file.
slide-19
SLIDE 19

FIRST PASS

  • Read each line of assembly
  • Break it into pieces or lexemes.
  • ble $t0, 100, loop (six lexemes)
  • If the line begins with a label, then record that label and its

instruction address in a symbol table.

slide-20
SLIDE 20

SYMBOL TABLE

A table that maps labels to addresses.

label

(example) address

swing hm, line 23 0xN...N pitch hm, line 76 0xN...N

slide-21
SLIDE 21

HOW DOES AN ASSEMBLER WORK?

  • Bind program labels to memory locations.
  • Translate assembly instructions to an object file.
slide-22
SLIDE 22

SECOND PASS

  • Examine each line of the instructions
  • If it contains an instruction, translate it to binary machine code
  • Replace local label references with addresses in the

symbol table

  • Leave instructions using external symbols unresolved.
  • The linker will help resolve addresses needed by these

instructions.

slide-23
SLIDE 23

WHAT IS AN OBJECT FILE, REALLY?

Structured encoding of your assembly instructions. Six segments.

  • bject file

header text segment data segment

relocation information

symbol table debugging info

slide-24
SLIDE 24

HEADER

  • bject file

header text segment data segment

relocation information

symbol table debugging info

Describes the size and position

  • f the other pieces of the file.
slide-25
SLIDE 25

TEXT

Contains machine language from the source file. May contain unresolved references! (therefore unexecutable)

  • bject file

header text segment data segment

relocation information

symbol table debugging info

.text

slide-26
SLIDE 26

DATA

Contains binary representation of data from your source file. May also contain unresolved references.

  • bject file

header text segment data segment

relocation information

symbol table debugging info

.data

slide-27
SLIDE 27

RELOCATION

Instructions and data that depend on specific, absolute addresses. These references have to change if portions of the program are moved in memory.

  • bject file

header text segment data segment

relocation information

symbol table debugging info

slide-28
SLIDE 28

RELOCATION

Instructions and data that depend on specific, absolute addresses.

  • bject file

header text segment data segment

relocation information

symbol table debugging info

“Grab a beverage, the third bottle on the third shelf.” If the whole shelf moves, you need to revise your specific instruction.

slide-29
SLIDE 29

RELOCATION

  • bject file

header text segment data segment

relocation information

symbol table debugging info

The assembler doesn’t know the final, specific memory locations a procedure or piece of data will occupy after it is linked with other object files. (The linker will determine where the shelf of beverages will be finally located.)

slide-30
SLIDE 30

SYMBOL TABLE

A mapping of labels to addresses, as well as unresolved references.

  • bject file

header text segment data segment

relocation information

symbol table

debugging info

slide-31
SLIDE 31

DEBUGGING INFORMATION

A description of how the program was compiled.

  • bject file

header text segment data segment

relocation information symbol table

debugging info

For a debugger/assembler can find which memory addresses correspond to which lines of source code.

(“Look at line 6” is better than “look at instruction 0xnnnnnnnn.”)

slide-32
SLIDE 32
  • Reading 19
  • Textbook sections B.6 & 2.8

HOMEWORK

FUNctions!