You are on page 1of 18

Lecture 4

LC-3 Assembly Language


CS Reality
G
Youve got to know assembly
G
Chances are, youll never write program in
assembly

Compilers are much better & more patient than


you are
G
n!erstan!ing assembly "ey to machine-level
e#ecution mo!el

$ehavior o% programs in presence o% bugs

&igh-level language mo!el brea"s !own

'uning program per%ormance

n!erstan!ing sources o% program ine%%iciency

(mplementing system so%tware

Compiler has machine co!e as target

)perating systems must manage process state


(ts har! to write co!e in *s & +s,
G
Assembly language ma"es it possible to write -achine
Language co!e

each line o% assembly language is translate! into a single


-L instruction
G
A program calle! the Assembler !oes the translation an!
provi!es use%ul tools.

use o% labels - symbolic names %or a!!ress locations

automatic conversion o% binary / he# / !ecimal

pseu!o-ops
Assembly Language (nstructions
G
0ormats

LA$1L )2C)31 )21RA43S 5 C)--14'S

LA$1L 2S13)-)2S 5 C)--14'S


G
)pco!e

Symbolic name %or the 4-bit -L opco!e


G
Label

Symbolic name %or a memory location6 (t is use! to.

in!icate the target o% a branch instruction, e6g6 A7A(4 in


location +$

in!icate the location o% a store! value or array, e6g6 4-$1R


an! S(8
G
Comments

inten!e! %or humans only. e#planation o% co!e, visual


!isplay
2seu!o-)ps 9

9 are !irectives to the assembler

they are not translate! into -L instructions

LC-3 2seu!o-)ps.

6)R(7 a!!ress 'ells assembler where to locate the program in


memory :starting a!!ress;6

60(LL value Store value in the ne#t location in memory6

6$L<= n Set asi!e a bloc" o% n wor!s in memory6

6S'R(47> string Store the string, one character per wor!, in


memory6 A!! a wor! o% #++++ a%ter the string6

6143 -ar"s the en! o% the source program :not to be


con%use! with the instruction &AL',;

618'1R4AL 'he label so in!icate! is allocate! in another


mo!ule6
A sample program
+* 5
+? 5 2rogram to multiply an integer by the number @
+3 5
+4 6)R(7 #3+A+
+A L3 R*, S(8
+@ L3 R?, 4-$1R
+B A43 R3, R3, C+ 5 clear R3 to hol! the pro!uct
+D 5
+E 5 'he inner loop
+A 5
+$ A7A(4 A33 R3, R3, R?
+C A33 R*, R*, C-* 5 "eep trac" o% iterations
+3 $Rp A7A(4
+1 5
+0 &AL'
*+ 5
** 4-$1R 6$L<= *
*? S(8 60(LL #+++@
*3 5
*4 6143
0rom Assembly to bits9
6)R(7 #3+++
A43 R*, R*, C+
A33 R*, R*, C*+
L3 R?, 'wenty
L3 R3, 1ss
&AL'
'wenty 0(LL #++*4
6$L<= ?
1ss 60(LL FSG
6S'R(47> F&iG
6$L<= 3
6143
#3+++. A43 R*, R*, + ++++
#3++*. A33 R*, R*, + *+*+
#3++?. L3 R?, + ++++ ++*+
#3++3. L3 R3, + ++++ +*++
#3++4. 'RA2 ++*+ +*+*
#3++A. ++++ ++++ +++* +*++ 5 #++*4
#3++@.
#3++B.
#3++D. ++++ ++++ +*+* ++** 5 #++A3 H IS
#3++E. ++++ ++++ +*++ *+++ 5 #++4D H I&
#3++A. ++++ ++++ +**+ *++* 5 #++@E H Ii
#3++$. ++++ ++++ ++++ ++++ 5 null terminator
#3++C.
#3++3.
#3++1.
'he Assembly 2rocess
G
)bJective

'ranslate the AL :Assembly Language; program into -L


:-achine Language;6

1ach AL instruction yiel!s one -L instruction wor!6

(nterpret pseu!o-ops correctly6


G
2roblem

An instruction may re%erence a label6

(% the label hasnt been encountere! yet, the assembler


canKt %orm the instruction wor!
G
Solution

'wo-pass assembly
'wo-2ass Assembly - *
G
0irst 2ass - generating the symbol
table

Scan each line

<eep trac" o% current a!!ress

(ncrement by * %or each instruction

A!Just as reLuire! %or any pseu!o-ops


:e6g6 60(LL or 6S'R(47>, etc6;

0or each label

1nter it into the symbol table

Allocate to it the current a!!ress

Stop when 6143 is encountere!


Symbol 'able e#ample

sing the earlier


e#ample.
;
; Program to multiply a number by six
;
.ORIG x3050
x3050 LD R1, I!
x3051 LD R", #$%&'R
x305" (#D R3, R3, )0
;
; *+e inner loop
;
x3053 (G(I# (DD R3, R3, R"
x305, (DD R1, R1, )-1
x3055 &Rp (G(I#
;
x305. /(L*
;
x3050 #$%&'R .&L12 1
x3053 I! .4ILL x000.
;
.'#D
Symbol Address
Again x3053
Number x3057
Six x3058
'wo-2ass Assembly - ?
G
Secon! 2ass - generating the -L
program

Scan each line again

'ranslate each AL instruction into -L

Loo" up symbols in the symbol table


instruction

1nsure that labels are no more than


M?A@ / -?AA lines %rom instruction

3etermine operan! %iel! %or the instruction

0ill memory locations as !irecte! by


pseu!o-ops

Stop when 6143 is encountere!


Assemble! co!e

sing the earlier


e#ample.
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, #0
x3053 0001 011 011 0 00 010 ; ADD R3, R3, R2
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 ; .BL! 1
x305" 0000 0000 0000 0110 ; .#ILL x0006
Symbol Address
Again x3053
Number x3057
Six x3058
1#ercise
6)R(7 #3+++
3+++ A43 R?, R?, #+
3++* L3( R*, (nput
3++? Count $RN Report
3++3 $Rp Shi%t
3++4 A33 R?, R?, #*
3++A Shi%t A33 R*, R*, R*
3++@ $RnNp Count
3++B Report A43 R3, R?, #*
3++D S'( R3, )utput
3++E 'RA2 #?A
3++A (nput 60(LL #3?++
3++$ )utput 60(LL #3?+*
Symbol Address
Count x3002
Shift x3005
Report x3007
Input x300A
utput x300!
0rom C to e#ecutable
G
2reprocessing phase. -o!i%ies original program accor!ing to
preprocessor !irectives :these start with the C character;6 'he
result is another C program te#t %ile :typically with the 6i su%%i#;

Cinclu!e Ost!io6hP

C!e%ine 0ALS1 +
G
Compilation phase. te#t %ile converte! %rom high-level language
to assembly6

Regar!less o% the original high-level language, all programs


han!le! i!entically %rom this point onwar!

)ne assembly correspon!s one-to-one with machine instructions


Hello.c Hello.i Hello.s Hello.o Hello
Pre-
Processor
(cpp)
Compiler
(cc1)
Assembler
(as)
Linker
(ld)
"ry running g55 -' and g55 - to emit $i and $s files respe%ti&ely$
0rom C to e#ecutable
G
Assembly phase. assembly te#t %ile converte! into
machine language binary %ile an! pac"age! into a
relocatable obJect program6
G
Lin"er phase. multiple obJect programs are merge! to
result in an e#ecutable obJect %ile6

0or e#ample. a stan!ar! library %unction such as print%


might resi!e in a separate precompile! obJect %ile :li"e
print%6o; that e#ists elsewhere in the system6
Hello.c Hello.i Hello.s Hello.o Hello
Pre-
Processor
(cpp)
Compiler
(cc1)
Assembler
(as)
Linker
(ld)
)bJect 0ile
G
1ach source %ile is translate! into an obJect %ile

(nitial memory content :instructions M variables;6

'he symbol table6


G
A complete program may inclu!e several source an!/or
obJect %iles.

Source %iles written in Assembly by the programmer

Library %iles provi!e! by the system :)S or other;

Compile! &LL :e6g6, C; libraries


G
'he obJect %iles must be lin"e!

)ne obJect %ile will be the FmainG

All cross-re%erence! labels in symbol tables will be resolve!


'he en! result 9
G
9 is the e#ecutable image :e6g6, 6e#e %ile;

this is a %ile :FimageG; o% the %inaliNe! list o% -L


instructions,
with all symbolic re%erences resolve!

it is loaded by copying the list into memory,


starting at the
a!!ress speci%ie! in the 6)R(7 !irective

it is run by copying the starting a!!ress to the 2C


Representing (nstructions
G
int sum(int x, int y)
G
{
G
return x+y;
G
}
Different machines use totally different instructions and encodings
00
00
30
42
Alpha sum
01
80
FA
6B
E0
08
81
C3
SPARC sum
90
02
00
09

For this example, Alpha & PA!C "se


t#o $-b%te instr"ctions

&se di''erin( n"mbers o' instr"ctions in other


cases

x)* "ses + instr"ctions #ith len(ths 1, ,, and -


b%tes

ame 'or ./ and 'or Lin"x

./ 0 Lin"x not '"ll% binar% compatible


E5
8B
55
89
x86 sum
45
0C
03
45
08
89
EC
5D
C3

You might also like