Transcript Slides10
Overview
• Problem 5.33
• Machine Language Programming Exercises
• Introduction to Assembly Language Programming
Examples of Assembly Language Programming
Writing Assembly Language Programming.
Homework Problem 5.33
The instructions are:
x3000
x3001
x3002
x3003
x3004
x3005
x3007
x3008
x3009
AND R7, R7, #0
ADD R6, R7, #1
AND R4, R5, R6
BRZ x3005
ADD R0, R0, #1
ADD R6, R6, R6
ADD R7, R7, #1
ADD R1, R7, #-8
BRN x3002
R7 <-0
R6 <-1
Uses R6 as mask to tests one bit of R5 for a 1
Skips x3004 if bit in R5 was not a 1
Increments R0 if tested bit in R5 was a 1
Moves test bit of R6 to left
Increments R7
R1 is –7, -6, -5, -4, -3, -2, -1, 0
Loops back 7 times, i.e. tests lowest 8 bits of R5
The number in R0 increases by the number of 1’s in R5. If R0 was initially 0, then R0
being 5 after completion means that there were five 1’s in the lower 8 bits of R5.
If the value in R0 was initially N, then there were 5-N 1’s in the lower 8 bits of R5.
Programming Exercise #1
Write a program to count the 1’s in register R0
•
Flow Diagram
•
Machine code
Programming Exercise #2
Write a program to add the contents of R0 and R1, and indicate in R2 if there
was an overflow
•
Flow Diagram
•
Machine code
Programming Exercise #3
Write a program to Divide the contents of R0 by the contents of R1
•
Flow Diagram
•
Machine code
Programming Exercise #4
Write a program to read characters from the keyboard, echo them on the
console, and pack them into a file (2 characters per word)
•
Flow Diagram
•
Machine code
LC-3 Assembly Language Syntax
•
•
•
Each line of a program is one of the following:
– an instruction
– an assember directive (or pseudo-op)
– a comment
Whitespace (between symbols) and case are ignored.
Comments (beginning with “;”) are also ignored.
•
An instruction has the following format:
LABEL OPCODE OPERANDS ;COMMENTS
optional
mandatory
An Assembly Language Program
;
; Program to multiply a number by the constant 6
;
.ORIG
x3050
LD
R1, SIX
LD
R2, NUMBER
AND
R3, R3, #0
; Clear R3. It will
; contain the product.
; The inner loop
;
AGAIN
ADD
R3, R3, R2
ADD
R1, R1, #-1
; R1 keeps track of
BRp
AGAIN
; the iteration.
;
HALT
;
NUMBER
.BLKW
1
SIX
.FILL
x0006
;
.END
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
Meaning
.ORIG
starting address of program
address
.END
end of program
.BLKW
n
allocate n words of storage
.FILL
n
allocate one word, initialize with value n
.STRINGZ
n-character
string
allocate n+1 locations,
initialize w/characters and null
terminator
Trap Codes
•
LC-3 assembler provides “pseudo-instructions” for
each trap code, so you don’t have to remember them.
Code
Equivalent
Description
HALT
TRAP x25
Halt execution and print message to console.
IN
TRAP x23
Print prompt on console,
read (and echo) one character from keybd.
Character stored in R0[7:0].
OUT
TRAP x21
Write one character (in R0[7:0]) to console.
GETC
TRAP x20
Read one character from keyboard.
Character stored in R0[7:0].
PUTS
TRAP x22
Write null-terminated string to console.
Address of string is in R0.
Sample Program
•
Count the occurrences of a character in a file.
Remember this?
Count = 0
(R2 = 0)
Done?
YES
(R1 ?= EOT)
Ptr = 1st file character
Convert count to
ASCII character
(R0 = x30, R0 = R2 + R0)
NO
(R3 = M[x3012])
Print count
YES
Match?
NO
(TRAP x21)
(R1 ?= R0)
Input char
from keybd
(TRAP x23)
HALT
Incr Count
Load char from file
(R2 = R2 + 1)
(R1 = M[R3])
Load next char from file
(R3 = R3 + 1, R1 = M[R3])
(TRAP x25)
Count the occurrences of a character in a file (1 0f 2).
;
;
;
;
;
;
;
;
;
Program to count occurrences of a character in a file.
Character to be input from the keyboard.
Result to be displayed on the monitor.
Program only works if no more than 9 occurrences are found.
Initialization
.ORIG
AND
LD
GETC
LDR
x3000
R2, R2, #0
R3, PTR
;
;
;
;
R2
R3
R0
R1
is counter, initially 0
is pointer to character file
gets input character
gets first character from file
R1, R3, #0
;
; Test character for end of file
;
TEST
ADD
R4, R1, #-4
; Test for EOT (ASCII x04)
BRz
OUTPUT
; 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
GETCHAR
; If no match, do not increment
ADD
R2, R2, #1
;
; Get next character from file.
;
GETCHAR
ADD
R3, R3, #1
; Point to next character.
LDR
R1, R3, #0
; R1 gets next char to test
BRnzp
TEST
Count the occurrences of a character in a file (2 of 2).
;
; Output the count.
;
OUTPUT
LD
R0, ASCII
; Load the ASCII template
ADD
R0, R0, R2
; Covert binary count to ASCII
OUT
; ASCII code in R0 is displayed.
HALT
; Halt machine
;
; Storage for pointer and ASCII template
;
ASCII
.FILL
x0030
; ASCII offset
PTR
.FILL
x4000
; PTR to character file
.END