Overview Problem 5 33 Machine Language Programming Exercises
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: x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3007 x 3008 x 3009 AND R 7, #0 ADD R 6, R 7, #1 AND R 4, R 5, R 6 BRZ x 3005 ADD R 0, #1 ADD R 6, R 6 ADD R 7, #1 ADD R 1, R 7, #-8 BRN x 3002 R 7 <-0 R 6 <-1 Uses R 6 as mask to tests one bit of R 5 for a 1 Skips x 3004 if bit in R 5 was not a 1 Increments R 0 if tested bit in R 5 was a 1 Moves test bit of R 6 to left Increments R 7 R 1 is – 7, -6, -5, -4, -3, -2, -1, 0 Loops back 7 times, i. e. tests lowest 8 bits of R 5 The number in R 0 increases by the number of 1’s in R 5. If R 0 was initially 0, then R 0 being 5 after completion means that there were five 1’s in the lower 8 bits of R 5. If the value in R 0 was initially N, then there were 5 -N 1’s in the lower 8 bits of R 5.
Programming Exercise #1 Write a program to count the 1’s in register R 0 • Flow Diagram • Machine code
Programming Exercise #2 Write a program to add the contents of R 0 and R 1, and indicate in R 2 if there was an overflow • Flow Diagram • Machine code
Programming Exercise #3 Write a program to Divide the contents of R 0 by the contents of R 1 • 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 x 3050 LD R 1, SIX LD R 2, NUMBER AND R 3, #0 ; Clear R 3. It will ; contain the product. ; The inner loop ; AGAIN ADD R 3, R 2 ADD R 1, #-1 ; R 1 keeps track of BRp AGAIN ; the iteration. ; HALT ; NUMBER. BLKW 1 SIX. FILL x 0006 ; . 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 x 25 Halt execution and print message to console. IN TRAP x 23 Print prompt on console, read (and echo) one character from keybd. Character stored in R 0[7: 0]. OUT TRAP x 21 Write one character (in R 0[7: 0]) to console. GETC TRAP x 20 Read one character from keyboard. Character stored in R 0[7: 0]. PUTS TRAP x 22 Write null-terminated string to console. Address of string is in R 0.
Sample Program • Count the occurrences of a character in a file. Remember this?
Count the occurrences of a character in a file (1 0 f 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 x 3000 R 2, #0 ; R 2 is counter, initially 0 R 3, PTR ; R 3 is pointer to character file ; R 0 gets input character R 1, R 3, #0 ; R 1 gets first character from file ; ; Test character for end of file ; TEST ADD R 4, R 1, #-4 ; Test for EOT (ASCII x 04) BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R 1, R 1 ADD R 1, R 0 ; If match, R 1 = x. FFFF NOT R 1, R 1 ; If match, R 1 = x 0000 BRnp GETCHAR ; If no match, do not increment ADD R 2, #1 ; ; Get next character from file. ; GETCHAR ADD R 3, #1 ; Point to next character. LDR R 1, R 3, #0 ; R 1 gets next char to test BRnzp TEST
Count the occurrences of a character in a file (2 of 2). ; ; Output the count. ; OUTPUT LD R 0, ASCII ; Load the ASCII template ADD R 0, R 2 ; Covert binary count to ASCII OUT ; ASCII code in R 0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII. FILL x 0030 ; ASCII offset PTR. FILL x 4000 ; PTR to character file. END
- Slides: 13