Understanding Assembly Language: From Code to Executable, Study notes of Computer Architecture and Organization

An overview of assembly language, its importance in computer organization, and the process of translating assembly language code into machine language. The basics of assembly language instructions, pseudo-ops, and the use of labels. It also explains the two-pass assembly process and the role of the symbol table. Wright state university's ceg 320/520 comp. Org. & assembly course is discussed as an example.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-28a
koofers-user-28a 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Chapter 7Chapter 7
LC-3 Assembly Language
2
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering CEG 320/520
Comp. Org. & Assembly
CS RealityCS Reality
You’ve got to know assembly
Chances are, you’ll never write program in assembly
Compilers are much better & more patient than you are
Understanding assembly key to machine-level execution model
Behavior of programs in presence of bugs
High-level language model breaks down
Tuning program performance
Understanding sources of program inefficiency
Implementing system software
Compiler has machine code as target
Operating systems must manage process state
3
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering CEG 320/520
Comp. Org. & Assembly
It’s hard to write code in 1’s & 0’s!It’s hard to write code in 1’s & 0’s!
Assembly language makes it possible to write Machine Language code
each line of assembly language is translated into a single ML instruction
A program called the Assembler does the translation and provides useful
tools:
use of labels - symbolic names for address locations
automatic conversion of binary / hex / decimal
pseudo-ops
4
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering CEG 320/520
Comp. Org. & Assembly
Assembly Language InstructionsAssembly Language Instructions
Formats
LABEL OPCODE OPERANDS ; COMMENTS
LABEL PSEUDO-OPS ; COMMENTS
Opcode
Symbolic name for the 4-bit ML opcode
Label
Symbolic name for a memory location. It is used to:
indicate the target of a branch instruction, e.g. AGAIN in location 0B
indicate the location of a stored value or array, e.g. NUMBER and SIX
Comments
intended for humans only: explanation of code, visual display
5
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering CEG 320/520
Comp. Org. & Assembly
PseudoPseudo--Ops … Ops …
… are directives to the assembler
they are not translated into ML instructions
LC-3 Pseudo-Ops:
.ORIG address Tells assembler where to locate the program in
memory (starting address).
.FILL value Store value in the next location in memory.
.BLKW n Set aside a block of n words in memory.
.STRINGZ string Store the string, one character per word, in
memory. Add a word of x0000 after the string.
.END Marks the end of the source program (not to be
confused with the instruction HALT!)
.EXTERNAL The label so indicated is allocated in another module.
6
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering CEG 320/520
Comp. Org. & Assembly
A sample programA sample program
01 ;
02 ; Program to multiply an integer by the number 6
03 ;
04 .ORIG x3050
05 LD R1, SIX
06 LD R2, NUMBER
07 AND R3, R3, #0 ; clear R3 to hold the product
08 ;
09 ; The inner loop
0A ;
0B AGAIN ADD R3, R3, R2
0C ADD R1, R1, #-1 ; keep track of iterations
0D BRp AGAIN
0E ;
0F HALT
10 ;
11 NUMBER .BLKW 1
12 SIX .FILL x0006
13 ;
14 .END
pf3
pf4

Partial preview of the text

Download Understanding Assembly Language: From Code to Executable and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter 7Chapter 7

LC-3 Assembly Language Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 2

CS RealityCS Reality

You’ve got to know assembly  Chances are, you’ll never write program in assembly

  • Compilers are much better & more patient than you are  Understanding assembly key to machine-level execution model
  • Behavior of programs in presence of bugs  High-level language model breaks down
  • Tuning program performance  Understanding sources of program inefficiency
  • Implementing system software  Compiler has machine code as target  Operating systems must manage process state Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 3

It’s hard to write code in 1’s & 0’s!It’s hard to write code in 1’s & 0’s!

 Assembly language makes it possible to write Machine Language code

  • each line of assembly language is translated into a single ML instruction  A program called the Assembler does the translation and provides useful tools:
  • use of labels - symbolic names for address locations
  • automatic conversion of binary / hex / decimal
  • pseudo-ops Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 4

Assembly Language InstructionsAssembly Language Instructions

 Formats

  • LABEL OPCODE OPERANDS ; COMMENTS
  • LABEL PSEUDO-OPS ; COMMENTS  Opcode
  • Symbolic name for the 4-bit ML opcode  Label
  • Symbolic name for a memory location. It is used to:  indicate the target of a branch instruction, e.g. AGAIN in location 0B  indicate the location of a stored value or array, e.g. NUMBER and SIX  Comments
  • intended for humans only: explanation of code, visual display Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 5

PseudoPseudo--Ops …Ops …

  • … are directives to the assembler  they are not translated into ML instructions
  • LC-3 Pseudo-Ops:  .ORIG address Tells assembler where to locate the program in memory (starting address).  .FILL value Store value in the next location in memory.  .BLKW n Set aside a block of n words in memory.  .STRINGZ string Store the string, one character per word, in memory. Add a word of x0000 after the string.  .END Marks the end of the source program (not to be confused with the instruction HALT!)  .EXTERNAL The label so indicated is allocated in another module. Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 6

A sample programA sample program

0102 ;; Program to multiply an integer by the number 6 03 ; 0405 .ORIGLD x3050R1, SIX 06 LD R2, NUMBER (^0708) ; AND R3, R3, #0 ; clear R3 to hold the product 09 ; The inner loop 0A 0B ;AGAIN ADD R3, R3, R 0C ADD R1, R1, #- 1 ; keep track of iterations 0D 0E BRp; AGAIN 0F HALT 1011 ;NUMBER .BLKW 1 12 SIX .FILL x 1314 ; .END

Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 7

From Assembly to bits…From Assembly to bits…

.ORIG x AND R1, R1, # ADD R1, R1, # LD R2, Twenty LD R3, Ess HALT Twenty FILL x .BLKW 2 Ess .FILL “S” .STRINGZ “Hi” .BLKW 3 .END x3000: AND R1, R1, 0 0000 x3001: ADD R1, R1, 0 1010 x3002: LD R2, 0 0000 0010 x3003: LD R3, 0 0000 0100 x3004: TRAP 0010 0101 x3005: 0000 0000 0001 0100 ; x x3006: x3007: x3008: 0000 0000 0101 0011 ; x0053 = ‘S’ x3009: 0000 0000 0100 1000 ; x0048 = ‘H’ x300A: 0000 0000 0110 1001 ; x0069 = ‘i’ x300B: 0000 0000 0000 0000 ; x0000 = null terminator x300C: x300D: x300E: Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 8

The Assembly ProcessThe Assembly Process

 Objective

  • Translate the AL (Assembly Language) program into ML (Machine Language).
  • Each AL instruction yields one ML instruction word.
  • Interpret pseudo-ops correctly.  Problem
  • An instruction may reference a label.
  • If the label hasn’t been encountered yet, the assembler can't form the instruction word  Solution
  • Two-pass assembly Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 9

TwoTwo--Pass AssemblyPass Assembly - - 11

First Pass - generating the symbol table

  • Scan each line
  • Keep track of current address  Increment by 1 for each instruction  Adjust as required for any pseudo-ops (e.g. .FILL or .STRINGZ, etc.)
  • For each label  Enter it into the symbol table  Allocate to it the current address
  • Stop when .END is encountered Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 10

Symbol Table exampleSymbol Table example

  • Using the earlier example: Symbol Address Again x Number x Six x ; ; Program to multiply a number by six ; .ORIG x x3050 LD R1, SIX x3051 x3052 LDAND R2, NUMBERR3, R3, # ; ; The inner loop ; x3053 AGAIN ADD R3, R3, R x3054 x3055 ADDBRp R1, R1, #AGAIN- 1 ; x3056 (^) ; HALT x3057 NUMBER .BLKW 1 x3058 SIX ; .FILL x .END Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 11

TwoTwo--Pass AssemblyPass Assembly - - 22

Second Pass - generating the ML program

  • Scan each line again
  • Translate each AL instruction into ML  Look up symbols in the symbol table instruction  Ensure that labels are no more than +256 / - 255 lines from instruction  Determine operand field for the instruction
  • Fill memory locations as directed by pseudo-ops
  • Stop when .END is encountered Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 12

Assembled codeAssembled code

  • Using the earlier example: Symbol Address Again x Number x Six x x3050 0010 001 0 0000 0111 ; LD R1, SIX x3051 0010 010 0 0000 0101 ; LD R2, NUMBER x3052 0101 011 011 1 00000 ; AND R3, R3, # x3053 0001 011 011 0 00 010 ; ADD R3, R3, R x3054 0001 001 001 1 11111 ; ADD R1, R1, #- 1 x3055 0000 001 1 1111 1101 ; BRp AGAIN x3056 1111 0000 0010 0101 ; HALT X3057 ; .BLKW 1 x3058 0000 0000 0000 0110 ; .FILL x

Wright State University, College of Engineering Dr. Doom, Computer Science & Engineering CEG 320/520 Comp. Org. & Assembly 19

ProblemsProblems

 7.1, 7.2 ( tricky ), 7.4, 7.5, 7.