Assembly Language Programming for Computing Layers

undefined
 
C
h
a
p
t
e
r
 
7
A
s
s
e
m
b
l
y
 
L
a
n
g
u
a
g
e
C
o
m
p
u
t
i
n
g
 
L
a
y
e
r
s
Problems
Language
Instruction Set Architecture 
Microarchitecture
Circuits
Devices
Algorithms
H
u
m
a
n
-
R
e
a
d
a
b
l
e
 
M
a
c
h
i
n
e
 
L
a
n
g
u
a
g
e
C
o
m
p
u
t
e
r
s
 
l
i
k
e
 
o
n
e
s
 
a
n
d
 
z
e
r
o
s
 
0
0
0
1
1
1
0
0
1
0
0
0
0
1
1
0
H
u
m
a
n
s
 
l
i
k
e
 
s
y
m
b
o
l
s
 
A
D
D
R
6
,
R
2
,
R
6
;
 
i
n
c
r
e
m
e
n
t
 
i
n
d
e
x
 
r
e
g
.
A
s
s
e
m
b
l
e
r
 
i
s
 
a
 
p
r
o
g
r
a
m
 
t
h
a
t
 
t
u
r
n
s
 
s
y
m
b
o
l
s
 
i
n
t
o
 
m
a
c
h
i
n
e
 
i
n
s
t
r
u
c
t
i
o
n
s
.
I
S
A
-
s
p
e
c
i
f
i
c
:
 
c
l
o
s
e
 
c
o
r
r
e
s
p
o
n
d
e
n
c
e
 
b
e
t
w
e
e
n
 
s
y
m
b
o
l
s
 
a
n
d
 
i
n
s
t
r
u
c
t
i
o
n
 
s
e
t
M
n
e
m
o
n
i
c
s
 
f
o
r
 
o
p
c
o
d
e
s
L
a
b
e
l
s
 
f
o
r
 
m
e
m
o
r
y
 
l
o
c
a
t
i
o
n
s
A
d
d
i
t
i
o
n
a
l
 
o
p
e
r
a
t
i
o
n
s
 
f
o
r
 
a
l
l
o
c
a
t
i
n
g
 
s
t
o
r
a
g
e
 
a
n
d
 
i
n
i
t
i
a
l
i
z
i
n
g
 
d
a
t
a
A
n
 
A
s
s
e
m
b
l
y
 
L
a
n
g
u
a
g
e
 
P
r
o
g
r
a
m
;
; Program to multiply a number by six
;
 
    
 
.ORIG
 
x3050
 
    
 
LD
 
R1, SIX      ; R1 has constant
 
    
 
LD
 
R2, NUMBER   ; R2 has variable
 
    
 
AND
 
R3, R3, #0   ; R3 has product
;
; The inner loop
;
AGAIN 
  
ADD
 
R3, R3, R2   ; R3 += R2
 
    
 
ADD
 
R1, R1, #-1  ; R1 is loop counter
  
BRp
 
AGAIN
 
   ; conditional branch
;
 
        HALT
;
NUMBER
  
.BLKW
 
1            ; variable
SIX
  
.FILL
 
x0006        ; constant
;
  
.END
L
C
-
3
 
A
s
s
e
m
b
l
y
 
L
a
n
g
u
a
g
e
 
S
y
n
t
a
x
E
a
c
h
 
l
i
n
e
 
o
f
 
a
 
p
r
o
g
r
a
m
 
i
s
 
o
n
e
 
o
f
 
t
h
e
 
f
o
l
l
o
w
i
n
g
:
I
n
s
t
r
u
c
t
i
o
n
A
s
s
e
m
b
l
e
r
 
d
i
r
e
c
t
i
v
e
 
(
o
r
 
p
s
e
u
d
o
-
o
p
)
C
o
m
m
e
n
t
W
h
i
t
e
s
p
a
c
e
 
(
b
e
t
w
e
e
n
 
s
y
m
b
o
l
s
)
 
a
n
d
 
c
a
s
e
 
a
r
e
 
i
g
n
o
r
e
d
.
C
o
m
m
e
n
t
s
 
(
b
e
g
i
n
n
i
n
g
 
w
i
t
h
 
;
)
 
a
r
e
 
a
l
s
o
 
i
g
n
o
r
e
d
.
x
 
h
e
x
,
 
#
 
d
e
c
i
m
a
l
,
 
d
e
f
a
u
l
t
 
i
s
 
d
e
c
i
m
a
l
A
n
 
i
n
s
t
r
u
c
t
i
o
n
 
h
a
s
 
t
h
e
 
f
o
l
l
o
w
i
n
g
 
f
o
r
m
a
t
:
LABEL OPCODE OPERANDS
 
; COMMENTS
optional
mandatory
O
p
c
o
d
e
s
 
a
n
d
 
O
p
e
r
a
n
d
s
O
p
c
o
d
e
s
r
e
s
e
r
v
e
d
 
s
y
m
b
o
l
s
 
t
h
a
t
 
c
o
r
r
e
s
p
o
n
d
 
t
o
 
L
C
-
3
 
i
n
s
t
r
u
c
t
i
o
n
s
l
i
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
E
O
T
 
(
x
0
4
)
A
t
 
t
h
e
 
e
n
d
,
 
p
r
i
n
t
 
t
h
e
 
n
u
m
b
e
r
 
o
f
 
c
h
a
r
a
c
t
e
r
s
 
a
n
d
 
h
a
l
t
(
a
s
s
u
m
e
 
t
h
e
r
e
 
w
i
l
l
 
b
e
 
l
e
s
s
 
t
h
a
n
 
1
0
 
o
c
c
u
r
r
e
n
c
e
s
 
o
f
 
t
h
e
 
c
h
a
r
a
c
t
e
r
)
A
 
s
p
e
c
i
a
l
 
c
h
a
r
a
c
t
e
r
 
u
s
e
d
 
t
o
 
i
n
d
i
c
a
t
e
 
t
h
e
 
e
n
d
 
o
f
 
a
 
s
e
q
u
e
n
c
e
 
i
s
 
o
f
t
e
n
 
c
a
l
l
e
d
 
a
 
s
e
n
t
i
n
e
l
.
Useful when you don
t know ahead of time how many times to execute a loop.
F
l
o
w
 
C
h
a
r
t
L
C
3
 
A
s
s
e
m
b
l
y
 
; Get next character from the file
;
GETC ADD    R3, R3, #1  ; Increment the pointer
         LDR    R1, R3, #0  ; R1 gets the next character to test
         BRnzp TEST
;
; Output the count.
;
OUT  LD       R0, ASCII   ; Load the ASCII template
        ADD     R0, R0, R2 ; Convert binary to ASCII
        TRAP    x21           ; ASCII code in R0 is displayed
        TRAP    x25           ; Halt machine
;
; Storage for pointer and ASCII template
;
ASCII .FILL   x0030
PTR   .FILL   x3014
         .END
        .ORIG   x3000
        AND    R2, R2, #0
 
; R2 is counter, initialize to 0
        LD      R3, PTR    
 
; R3 is pointer to characters
        TRAP  x23   
  
; R0 gets character input
        LDR     R1 ,R3, #0 
 
; R1 gets the next character
;
; Test character for end of file
;
TEST ADD  R4, R1, #-4 
 
; Test for EOT
        BRz   OUT    
  
; If done, prepare the output
;
; Test character for match.  If a match, increment count.
;
        NOT     R1, R1
        ADD     R1, R1, R0
 
; If match, R1 = xFFFF
        NOT     R1, R1   
 
; If match, R1 = x0000
 
        BRnp    GETC   
 
; no match, do not increment
        ADD     R2, R2, #1
A
B
C
D
E
2) What instruction dereferences a
pointer to load a character into a
register?
1) What instruction would need to be
changed if the null terminator (0) is
used to signal the end of the file
L
C
3
 
A
s
s
e
m
b
l
y
 
; Get next character from the file
;
GETC ADD    R3, R3, #1  ; Increment the pointer
         LDR    R1, R3, #0  ; R1 gets the next character to test
         BRnzp TEST
;
; Output the count.
;
OUT  LD       R0, ASCII   ; Load the ASCII template
        ADD     R0, R0, R2 ; Convert binary to ASCII
        TRAP    x21           ; ASCII code in R0 is displayed
        TRAP    x25           ; Halt machine
;
; Storage for pointer and ASCII template
;
ASCII .FILL   x0030
PTR   .FILL   x3014
         .END
        .ORIG   x3000
        AND    R2, R2, #0
 
; R2 is counter, initialize to 0
        LD      R3, PTR    
 
; R3 is pointer to characters
        TRAP  x23   
  
; R0 gets character input
        LDR     R1 ,R3, #0 
 
; R1 gets the next character
;
; Test character for end of file
;
TEST ADD  R4, R1, #-4 
 
; Test for EOT
        BRz   OUT    
  
; If done, prepare the output
;
; Test character for match.  If a match, increment count.
;
        NOT     R1, R1
        ADD     R1, R1, R0
 
; If match, R1 = xFFFF
        NOT     R1, R1   
 
; If match, R1 = x0000
 
        BRnp    GETC   
 
; no match, do not increment
        ADD     R2, R2, #1
A
B
C
D
E
4) What check/test could be
accomplished with a NOT then an
ADD of 1 then an ADD
3) What instruction loads the PC with
the value found at an address in the
first x100 locations of memory
A
s
s
e
m
b
l
y
 
P
r
o
c
e
s
s
C
o
n
v
e
r
t
 
a
s
s
e
m
b
l
y
 
l
a
n
g
u
a
g
e
 
f
i
l
e
 
(
.
a
s
m
)
 
i
n
t
o
 
a
n
 
e
x
e
c
u
t
a
b
l
e
 
f
i
l
e
 
(
.
o
b
j
)
 
f
o
r
 
t
h
e
L
C
-
3
 
s
i
m
u
l
a
t
o
r
.
F
i
r
s
t
 
P
a
s
s
:
S
c
a
n
 
p
r
o
g
r
a
m
 
f
i
l
e
F
i
n
d
 
a
l
l
 
l
a
b
e
l
s
 
a
n
d
 
c
a
l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
 
i
s
 
.
B
L
K
W
 
o
r
 
.
S
T
R
I
N
G
Z
,
i
n
c
r
e
m
e
n
t
 
L
C
 
b
y
 
t
h
e
 
n
u
m
b
e
r
 
o
f
 
w
o
r
d
s
 
a
l
l
o
c
a
t
e
d
.
3.
S
t
o
p
 
w
h
e
n
 
.
E
N
D
 
s
t
a
t
e
m
e
n
t
 
i
s
 
r
e
a
c
h
e
d
.
N
O
T
E
:
 
A
 
l
i
n
e
 
t
h
a
t
 
c
o
n
t
a
i
n
s
 
o
n
l
y
 
a
 
c
o
m
m
e
n
t
 
i
s
 
c
o
n
s
i
d
e
r
e
d
 
a
n
 
e
m
p
t
y
 
l
i
n
e
.
P
r
a
c
t
i
c
e
C
o
n
s
t
r
u
c
t
 
t
h
e
 
s
y
m
b
o
l
 
t
a
b
l
e
 
f
o
r
 
t
h
e
 
p
r
o
g
r
a
m
 
i
n
 
F
i
g
u
r
e
 
7
.
1
(
S
l
i
d
e
s
 
7
-
1
2
 
t
h
r
o
u
g
h
 
7
-
1
4
)
.
S
y
m
b
o
l
 
T
a
b
l
e
C
o
n
s
t
r
u
c
t
 
t
h
e
 
s
y
m
b
o
l
 
t
a
b
l
e
 
f
o
r
 
t
h
e
 
p
r
o
g
r
a
m
  
 
.
ORIG    x3000
      
 
AND  R2, R2, #0   ; init counter
     
 
LD   R3, PTR      ; R3  pointer to chars
     
 
GETC              ; R0 gets char input
     
 
LDR  R1, R3, #0   ; R1 gets first char
TEST  
 
ADD  R4, R1, #-4  ; Test for EOT
     
 
BRz  OUTPUT       ; done?
;
;Test character for match, if so increment count.
        
 
NOT  R1, R1
        
 
ADD  R1, R1, R0   ; If match, R1 = xFFFF
       
 
NOT  R1, R1       ; If match, R1 = x0000
        
 
BRnp GETCHAR      ; No match, no increment
        
 
ADD   R2, R2, #1
;
; Get next character from file.
GETCHAR 
 
ADD   R3, R3, #1  ; Point to next cha.
        
 
LDR   R1, R3, #0  ; R1 gets next char
        
 
BRnzp TEST
;
; Output the count.
OUTPUT 
 
LD    R0, ASCII   ; Load ASCII template
       
 
ADD   R0, R0, R2  ; Covert binary to ASCII
       
 
OUT               ; ASCII code is displayed
 
HALT              ; Halt machine
;
; Storage for pointer and ASCII template
ASCII     .FILL   x0030
PTR       .FILL   x4000
          .END
S
e
c
o
n
d
 
P
a
s
s
:
 
G
e
n
e
r
a
t
i
n
g
 
M
a
c
h
i
n
e
 
L
a
n
g
u
a
g
e
F
o
r
 
e
a
c
h
 
e
x
e
c
u
t
a
b
l
e
 
a
s
s
e
m
b
l
y
 
l
a
n
g
u
a
g
e
 
s
t
a
t
e
m
e
n
t
,
 
g
e
n
e
r
a
t
e
 
t
h
e
c
o
r
r
e
s
p
o
n
d
i
n
g
 
m
a
c
h
i
n
e
 
l
a
n
g
u
a
g
e
 
i
n
s
t
r
u
c
t
i
o
n
.
I
f
 
o
p
e
r
a
n
d
 
i
s
 
a
 
l
a
b
e
l
,
 
l
o
o
k
 
u
p
 
t
h
e
 
a
d
d
r
e
s
s
 
f
r
o
m
 
t
h
e
 
s
y
m
b
o
l
 
t
a
b
l
e
.
P
o
t
e
n
t
i
a
l
 
p
r
o
b
l
e
m
s
:
I
m
p
r
o
p
e
r
 
n
u
m
b
e
r
 
o
r
 
t
y
p
e
 
o
f
 
a
r
g
u
m
e
n
t
s
e
x
:
N
O
T
R
1
,
#
7
A
D
D
R
1
,
R
2
A
D
D
R
3
,
R
3
,
N
U
M
B
E
R
I
m
m
e
d
i
a
t
e
 
a
r
g
u
m
e
n
t
 
t
o
o
 
l
a
r
g
e
e
x
:
A
D
D
R
1
,
R
2
,
#
1
0
2
3
A
d
d
r
e
s
s
 
(
a
s
s
o
c
i
a
t
e
d
 
w
i
t
h
 
l
a
b
e
l
)
 
m
o
r
e
 
t
h
a
n
 
2
5
6
 
f
r
o
m
 
i
n
s
t
r
u
c
t
i
o
n
c
a
n
t
 
u
s
e
 
P
C
-
r
e
l
a
t
i
v
e
 
a
d
d
r
e
s
s
i
n
g
 
m
o
d
e
P
r
a
c
t
i
c
e
U
s
i
n
g
 
t
h
e
 
s
y
m
b
o
l
 
t
a
b
l
e
 
c
o
n
s
t
r
u
c
t
e
d
 
e
a
r
l
i
e
r
,
 
t
r
a
n
s
l
a
t
e
 
t
h
e
s
e
 
s
t
a
t
e
m
e
n
t
s
 
i
n
t
o
L
C
-
3
 
m
a
c
h
i
n
e
 
l
a
n
g
u
a
g
e
.
.
ORIG    x3000
AND  R2, R2, #0   ; init counter
LD   R3, PTR      ; R3  pointer to chars
GETC              ; R0 gets char input
 
Symbol ptr: x3013, LD is at x3001
Offset needed: x13 - x02 (PC incremented)
L
C
-
3
 
A
s
s
e
m
b
l
e
r
U
s
i
n
g
 
a
s
s
e
m
b
l
e
 
(
U
n
i
x
)
 
o
r
 
L
C
3
E
d
i
t
 
(
W
i
n
d
o
w
s
)
,
 
g
e
n
e
r
a
t
e
s
 
s
e
v
e
r
a
l
 
d
i
f
f
e
r
e
n
t
o
u
t
p
u
t
 
f
i
l
e
s
.
This one gets
loaded into the
simulator.
S
h
o
w
 
b
i
n
a
r
y
 
v
e
r
s
i
o
n
 
o
f
 
f
i
l
e
:
 
x
x
d
 
b
 
<
f
i
l
e
>
S
h
o
w
 
h
e
x
 
v
e
r
s
i
o
n
 
o
f
 
f
i
l
e
:
 
x
x
d
 
<
f
i
l
e
>
O
b
j
e
c
t
 
F
i
l
e
 
F
o
r
m
a
t
L
C
-
3
 
o
b
j
e
c
t
 
f
i
l
e
 
c
o
n
t
a
i
n
s
S
t
a
r
t
i
n
g
 
a
d
d
r
e
s
s
 
(
l
o
c
a
t
i
o
n
 
w
h
e
r
e
 
p
r
o
g
r
a
m
 
m
u
s
t
 
b
e
 
l
o
a
d
e
d
)
,
 
f
o
l
l
o
w
e
d
 
b
y
M
a
c
h
i
n
e
 
i
n
s
t
r
u
c
t
i
o
n
s
E
x
a
m
p
l
e
B
e
g
i
n
n
i
n
g
 
o
f
 
c
o
u
n
t
 
c
h
a
r
a
c
t
e
r
 
o
b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
 
2
5
4
)
.
NOP
T
h
e
 
a
u
t
h
o
r
s
 
h
a
d
 
i
m
p
l
e
m
e
n
t
e
d
 
t
h
e
 
a
l
l
 
0
 
i
n
s
t
r
u
c
t
i
o
n
 
(
B
R
n
z
p
 
w
i
t
h
 
o
f
f
s
e
t
 
0
)
 
s
o
t
h
a
t
 
i
t
 
i
s
 
a
 
N
O
P
.
I
n
 
t
h
e
 
m
o
d
i
f
i
e
d
 
t
o
o
l
s
 
t
h
e
 
i
n
s
t
r
u
c
t
i
o
n
 
i
s
 
i
l
l
e
g
a
l
.
A
 
N
O
P
 
i
s
 
s
o
m
e
t
i
m
e
s
 
u
s
e
d
 
f
o
r
 
i
n
s
e
r
t
i
n
g
 
d
e
l
a
y
s
.
 
What are the values in R0,R1,R2 after
the code below executes? Assume the
Main label is at address x3000.
A.
x4321, x3003, x7324
B.
x4321, x3004, x7324
C.
x4321, x3003, x4321
D.
x4321, x3004, x4321
E.
None of the above
Main
  
LD R0,Data
   
LEA R1,Data
   
LDR R2,R1,0
   
HALT
Data   .FILL 0x4321
 
What is the PC offset field in the ST instruction shown
below?
A.
0b111111011
B.
0b111111100
C.
0b111111101
D.
0b100000100
E.
0b100000101
Data0
 
.FILL x1234
Data1
 
.FILL x2345
Data2
 
.BLKW 1
Main
  
LD R1,Data0
   
LD R2, Data1
   
ADD R3,R2,R1
   
ST R3,Data2
   
 
Match the 
LD R0, Var 
assembly instruction to the
corresponding C statement
A.
R0 = Var;
B.
R0 = &Var;
C.
R0 = *Ptr;
D.
None of the above
E.
All of the above
LC3 Assembly Code:
   
.ORIG 
 
x3000
Var 
 
.FILL
  
x0004
Ptr 
 
.FILL 
 
x3000
 
   
LD R0, Var
 
  
C Code:
int Var = 4;
int *Ptr = &Var;
int R0;
// what goes here?
 
Match the 
LEA R0, Var 
assembly instruction to
the corresponding C statement
A.
R0 = Var;
B.
R0 = &Var;
C.
R0 = *Ptr;
D.
None of the above
E.
All of the above
LC3 Assembly Code:
   
.ORIG 
 
x3000
Var 
 
.FILL
  
x0004
Ptr 
 
.FILL 
 
x3000
 
   
LEA R0, Var
 
  
C Code:
int Var = 4;
int *Ptr = &Var;
int R0;
// what goes here?
 
Match the 
LDI R0, Ptr 
assembly instruction to
the corresponding C statement
A.
R0 = Var;
B.
R0 = &Var;
C.
R0 = *Ptr;
D.
None of the above
E.
All of the above
LC3 Assembly Code:
   
.ORIG 
 
x3000
Var 
 
.FILL
  
x0004
Ptr 
 
.FILL 
 
x3000
 
   
LDI R0, Ptr
 
  
C Code:
int Var = 4;
int *Ptr = &Var;
int R0;
// what goes here?
 
Match the 
LD R1, PTR and LDR R0, R1, #0
assembly instructions to the corresponding C statement
A.
R0 = var;
B.
R0 = &Var;
C.
R0 = *Ptr;
D.
R1 = Ptr;
R0 = *R1;
E.
None of the above
LC3 Assembly Code:
   
.ORIG 
 
x3000
Var 
 
.FILL
  
x0004
Ptr 
 
.FILL 
 
x3000
 
   
LD R1, PTR
   
LDR R0, R1, #0
  
C Code:
int Var = 4;
int *Ptr = &Var;
int R0, R1;
// what goes here?
Slide Note
Embed
Share

Assembly language is a low-level programming language that enables direct interaction with a computer's hardware components. This content explores the fundamentals of assembly language, the relationship between human-readable machine language and binary code, an assembly language program for multiplying a number by six, LC-3 assembly language syntax, opcodes and operands concepts, and the importance of labels and comments in writing efficient assembly language programs.


Uploaded on Sep 21, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 7 Assembly Language

  2. Computing Layers Problems Algorithms Language Instruction Set Architecture Microarchitecture Circuits Devices

  3. Human-Readable Machine Language Computers like ones and zeros 0001110010000110 Humans like symbols ADD R6,R2,R6 ; increment index reg. Assembler is a program that turns symbols into machine instructions. ISA-specific: close correspondence between symbols and instruction set Mnemonics for opcodes Labels for memory locations Additional operations for allocating storage and initializing data

  4. An Assembly Language Program ; ; Program to multiply a number by six ; .ORIG LD LD AND ; ; The inner loop ; AGAIN ADD ADD BRp ; HALT ; NUMBER .BLKW SIX .FILL ; .END x3050 R1, SIX ; R1 has constant R2, NUMBER ; R2 has variable R3, R3, #0 ; R3 has product R3, R3, R2 ; R3 += R2 R1, R1, #-1 ; R1 is loop counter AGAIN ; conditional branch 1 ; variable x0006 ; constant

  5. LC-3 Assembly Language Syntax Each line of a program is one of the following: Instruction Assembler directive (or pseudo-op) Comment Whitespace (between symbols) and case are ignored. Comments (beginning with ; ) are also ignored. x hex, # decimal, default is decimal An instruction has the following format: LABEL OPCODE OPERANDS ; COMMENTS optional mandatory

  6. Opcodes and Operands Opcodes reserved symbols that correspond to LC-3 instructions listed in Appendix A ex: ADD, AND, LD, LDR, Operands registers -- specified by Rn, where n is the register number numbers -- indicated by # (decimal) or x (hex), default is decimal label -- symbolic name of memory location separated by comma number, order, and type correspond to instruction format ex: ADD R1,R1,R3 ADD R1,R1,#3 LD R6,NUMBER BRz LOOP

  7. Labels and Comments Label Placed at the beginning of the line Assigns a symbolic name to the address corresponding to line ex: LOOP ADD R1,R1,#-1 BRp LOOP Comment Anything after a semicolon is a comment Ignored by assembler Used by humans to document/understand programs Tips for useful comments: Avoid restating the obvious, as decrement R1 Provide additional insight, as in accumulate product in R6 Use comments to separate pieces of program State the purpose of each register

  8. Assembler Directives Pseudo-operations do not refer to operations executed by program used by assembler look like instruction, but opcode starts with dot Opcode Operand address Meaning starting address of program end of program allocate n words of storage .ORIG .END .BLKW n n allocate one word, initialize with value n allocate n+1 locations, initialize w/characters and null terminator .FILL n-character string .STRINGZ

  9. Trap Codes LC-3 assembler provides pseudo-instructions for each trap code, so you don t have to remember them. Unlike pseudo-operations pseudo-instructions do generate operations that will be executed by the program Code HALT Equivalent TRAP x25 Description Halt execution and print message to console. Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. Write one character (in R0[7:0]) to console. IN TRAP x23 OUT TRAP x21 Read one character from keyboard. Character stored in R0[7:0]. Write null-terminated string to console. Address of string is in R0. GETC TRAP x20 PUTS TRAP x22

  10. Style Guidelines Use the following style guidelines to improve the readability and understandability of your programs: 1. Provide a program header, with author s name, date, etc., and purpose of program. 2. Start labels, opcode, operands, and comments in same column for each line. (Unless entire line is a comment.) 3. Use comments to explain what each register does. 4. Give explanatory comment for most instructions. 5. Use meaningful symbolic names. Mixed upper and lower case for readability. ASCIItoBinary, InputRoutine, SaveR1 6. Provide comments between program sections. 7. Each line must fit on the page -- no wraparound or truncations. Long statements split in aesthetically pleasing manner.

  11. Example Count the occurrences of a character in an array Program begins at location x3000 Read character from keyboard Load each character from array Starting address of array is stored in the memory location immediately after the program If character equals input character, increment counter The end of the array is indicated by a special ASCII value: EOT (x04) At the end, print the number of characters and halt (assume there will be less than 10 occurrences of the character) A special character used to indicate the end of a sequence is often called a sentinel. Useful when you don t know ahead of time how many times to execute a loop.

  12. Flow Chart Count = 0 (R2 = 0) Convert count to ASCII character (R0 = x30, R0 = R2 + R0) YES Done? (R1 ?= EOT) Ptr = 1st file character (R3 = M[x3012]) NO Print count (TRAP x21) Match? (R1 ?= R0) YES NO Input char from keybd (TRAP x23) HALT (TRAP x25) Incr Count (R2 = R2 + 1) Load char from file (R1 = M[R3]) Load next char from file (R3 = R3 + 1, R1 = M[R3])

  13. LC3 Assembly .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23 LDR R1 ,R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 BRz OUT ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 NOT R1, R1 BRnp GETC ADD R2, R2, #1 ; Get next character from the file ; GETC ADD R3, R3, #1 ; Increment the pointer LDR R1, R3, #0 ; R1 gets the next character to test BRnzp TEST ; ; Output the count. ; OUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary to ASCII TRAP x21 ; ASCII code in R0 is displayed TRAP x25 ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x3014 .END ; R2 is counter, initialize to 0 ; R3 is pointer to characters ; R0 gets character input ; R1 gets the next character D A B ; Test for EOT ; If done, prepare the output E C ; If match, R1 = xFFFF ; If match, R1 = x0000 ; no match, do not increment 1) What instruction would need to be changed if the null terminator (0) is used to signal the end of the file 2) What instruction dereferences a pointer to load a character into a register?

  14. LC3 Assembly .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23 LDR R1 ,R3, #0 ; ; Test character for end of file ; TEST ADD R4, R1, #-4 BRz OUT ; ; Test character for match. If a match, increment count. ; NOT R1, R1 ADD R1, R1, R0 NOT R1, R1 BRnp GETC ADD R2, R2, #1 ; Get next character from the file ; GETC ADD R3, R3, #1 ; Increment the pointer LDR R1, R3, #0 ; R1 gets the next character to test BRnzp TEST ; ; Output the count. ; OUT LD R0, ASCII ; Load the ASCII template ADD R0, R0, R2 ; Convert binary to ASCII TRAP x21 ; ASCII code in R0 is displayed TRAP x25 ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII .FILL x0030 PTR .FILL x3014 .END ; R2 is counter, initialize to 0 ; R3 is pointer to characters ; R0 gets character input ; R1 gets the next character D A B ; Test for EOT ; If done, prepare the output E C ; If match, R1 = xFFFF ; If match, R1 = x0000 ; no match, do not increment 4) What check/test could be accomplished with a NOT then an ADD of 1 then an ADD 3) What instruction loads the PC with the value found at an address in the first x100 locations of memory

  15. Assembly Process Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator. First Pass: Scan program file Find all labels and calculate the corresponding addresses. Use these to build the symbol table Second Pass: Convert instructions to machine language, using information from symbol table

  16. First Pass: Constructing the Symbol Table 1. Find the .ORIG statement, which tells us the address of the first instruction. Initialize location counter (LC), which keeps track of the memory address of the current instruction. 2. For each non-empty line in the program: a) If line contains a label, add label and LC to symbol table. b) Increment LC. NOTE: If statement is .BLKW or .STRINGZ, increment LC by the number of words allocated. 3. Stop when .END statement is reached. NOTE: A line that contains only a comment is considered an empty line.

  17. .ORIG x3000 Symbol Table Construct the symbol table for the program TEST ; ;Test character for match, if so increment count. NOT R1, R1 ADD R1, R1, R0 ; If match, R1 = xFFFF NOT R1, R1 ; If match, R1 = x0000 BRnp GETCHAR ; No match, no increment ADD R2, R2, #1 ; ; Get next character from file. GETCHAR ADD R3, R3, #1 ; Point to next cha. LDR R1, R3, #0 ; R1 gets next char BRnzp TEST ; ; Output the count. OUTPUT LD R0, ASCII ; Load ASCII template ADD R0, R0, R2 ; Covert binary to ASCII OUT ; ASCII code is displayed HALT ; Halt machine ; ; Storage for pointer and ASCII template ASCII .FILL x0030 PTR .FILL x4000 .END AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input LDR R1, R3, #0 ; R1 gets first char ADD R4, R1, #-4 ; Test for EOT BRz OUTPUT ; done? Symbol TEST GETCHAR OUTPUT ASCII PTR Address x3004 x3013

  18. Second Pass: Generating Machine Language For each executable assembly language statement, generate the corresponding machine language instruction. If operand is a label, look up the address from the symbol table. Potential problems: Improper number or type of arguments ex: NOT R1,#7 ADD R1,R2 ADD R3,R3,NUMBER Immediate argument too large ex: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction can t use PC-relative addressing mode

  19. Practice Using the symbol table constructed earlier, translate these statements into LC-3 machine language. .ORIG x3000 AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input Symbol TEST PTR Address x3004 x3013 Statement Machine Language 0010 011 0 0001 0001 LD R3,PTR Symbol ptr: x3013, LD is at x3001 Offset needed: x13 - x02 (PC incremented)

  20. LC-3 Assembler Using assemble (Unix) or LC3Edit (Windows), generates several different output files. This one gets loaded into the simulator. Show binary version of file: xxd b <file> Show hex version of file: xxd <file>

  21. Object File Format LC-3 object file contains Starting address (location where program must be loaded), followed by Machine instructions Example Beginning of count character object file looks like this: .ORIG x3000 AND R2, R2, #0 ; init counter LD R3, PTR ; R3 pointer to chars GETC ; R0 gets char input .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23

  22. Multiple Object Files An object file is not necessarily a complete program. system-provided library routines code blocks written by multiple developers LC-3 simulator can load multiple object files into memory, then start executing at a desired address. System routines, such as keyboard input, are loaded automatically Loaded into system memory, below x3000 User code should be loaded between x3000 and xFDFF IO registers above xFDFF Each object file includes a starting address Be careful not to load overlapping object files

  23. Linking and Loading Loading is the process of copying an executable image into memory. More sophisticated loaders are able to relocate images to fit into available memory Must readjust branch targets, load/store addresses Linking is the process of resolving symbols between independent object files. Suppose we declare a label/variable in one module, and want to use it in another Some notation, such as .EXTERNAL, is used to tell assembler that the symbol is defined in another module Linker will search symbol tables of other modules to resolve symbols and complete code generation before loading For fun try creating an LC3 program that uses multiple .obj files More info: https://www.linuxjournal.com/article/6463 Video about how this works with C/C++ programs in Linux: https://youtu.be/dOfucXtyEsU

  24. LC-3 tools Local Modifications The following LC-3 assembly instructions will only work with the local tools in the CS department (they will not work with the tools at the text book web site). Pseudo-instructions: macros that are replaced by one or more actual machine instructions during assembly. .ZERO DR (AND DR,DR,#0) .COPY DR,SR1 (ADD DR,SR1,#0) GETS (Trap #26) NEWLN (Trap #27) Recall, pseudo-operations do not refer to operations executed by program, only used by assembler, and by convention start with a .

  25. LC-3 tools Local Modifications Instruction set extension: (remember there was one unused opcode) PUSH POP The authors had chosen to not implement these in accordance with the minimalist RISC approach (see page 254). NOP The authors had implemented the all 0 instruction (BRnzp with offset 0) so that it is a NOP. In the modified tools the instruction is illegal. A NOP is sometimes used for inserting delays.

  26. What are the values in R0,R1,R2 after the code below executes? Assume the Main label is at address x3000. A. x4321, x3003, x7324 B. x4321, x3004, x7324 C. x4321, x3003, x4321 D. x4321, x3004, x4321 E. None of the above Main LD R0,Data LEA R1,Data LDR R2,R1,0 HALT Data .FILL 0x4321

  27. What is the PC offset field in the ST instruction shown below? Data0 .FILL x1234 Data1 .FILL x2345 Data2 .BLKW 1 Main LD R1,Data0 LD R2, Data1 ADD R3,R2,R1 ST R3,Data2 A. 0b111111011 B. 0b111111100 C. 0b111111101 D. 0b100000100 E. 0b100000101

  28. Match the LD R0, Var assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LD R0, Var C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  29. Match the LEA R0, Var assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LEA R0, Var C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  30. Match the LDI R0, Ptr assembly instruction to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LDI R0, Ptr C Code: int Var = 4; int *Ptr = &Var; int R0; // what goes here? A. R0 = Var; B. R0 = &Var; C. R0 = *Ptr; D. None of the above E. All of the above

  31. Match the LD R1, PTR and LDR R0, R1, #0 assembly instructions to the corresponding C statement LC3 Assembly Code: .ORIG x3000 Var .FILL x0004 Ptr .FILL x3000 LD R1, PTR LDR R0, R1, #0 C Code: int Var = 4; int *Ptr = &Var; int R0, R1; // what goes here? A. R0 = var; B. R0 = &Var; C. R0 = *Ptr; D. R1 = Ptr; R0 = *R1; E. None of the above

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#