LC-3 Assembly Language Programming Examples

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to LC-3 Assembly Language Assembly Language Assembly Process Using the Editor & Simulator for Assembly Language Programming.
Advertisements

1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
PART 6 Programming 1.Decomposition 2.Three Constructs 3.Counting Example 4.Debugging.
Chapter 7 Assembly Language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 7-2 Human-Readable Machine Language.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
Chapter 7 Assembly Language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 7-2 Human-Readable Machine Language.
Overview Program in LC-3 machine language Use the Editor
Some thoughts: If it is too good to be true, it isn’t. Success is temporary. It is hard work to make it simple. Knowing you did it right is enough reward.
Overview I/O – memory mapped programmed / interrupt driven Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
Overview Review Trap Instruction Program in LC-3 machine language Use LC-3 Simulator.
Chapter 9 Overview Traps mechanism & RET Subroutines & JSR & JSRR & RET Interrupt mechanism & RTI.
Chapter 6 Programming in Machine Language The LC-3 Simulator
Introduction to LC-3 Assembly Language. LC-3 Assembly Language Syntax Each line of a program is one of the following: –an instruction –an assember directive.
Overview Intro to Project 2 - Serial I/O – RS232, USB Assembly Language Programming Using the LC-3 Simulator.
Introduction to LC-3 Assembly Language
Chapter 9 Trap Routines & RET Subroutines (or Functions) & JSR & JSRR & RET.
Overview Projects The Assembly Process Programmed I/O Interrupt Driven I/O.
Chapter 8 Overview Programmed I/O Introduction to Interrupt Driven I/O Project 3.
Chapter 8 I/O Programming Chapter 9 Trap Service Routines Programmed I/O Interrupts Interrupt Driven I/O Trap Service Routines.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Computer Science 210 s1c Computer Systems Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared.
Introduction to Computer Engineering CS/ECE 252, Spring 2007 Prof. Mark D. Hill Computer Sciences Department University of Wisconsin – Madison.
Introduction to Computing Systems and Programming Assembly Language.
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
Chapter 7 Assembly Language. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Our Bag of Tricks so far Control.
Introduction to Computer Engineering CS/ECE 252, Fall 2007 Prof. Mark D. Hill Computer Sciences Department University of Wisconsin – Madison.
Chapter 7 Assembly Language. 7-2 Human-Readable Machine Language Computers like ones and zeros… Humans like symbols… Assembler is a program that turns.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Introduction to Computing Systems and Programming The LC-2.
Computer Science 210 s1c Computer Systems Semester 1 Lecture Notes James Goodman (revised by Robert Sheehan) Credits: “McGraw-Hill” slides prepared.
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
Chapter 7 & 9.2 Assembly Language
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
Chapter 5 The LC-3.
LC-3 Details and Examples
Chapter 7 LC-2 Assembly Language.
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
COSC121: Computer Systems
Introduction to Computer Engineering
Introduction to Computer Engineering
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Chapter 7 Assembly Language
Chapter 5 The LC-3.
Chapter 9 TRAP Routines and Subroutines
Presentation transcript:

LC-3 Assembly Language Programming Examples

Sample Program Count the occurrences of a character in a file.

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 x3000 AND R2, R2, #0 ; R2 is counter, initially 0 LD R3, PTR ; R3 is pointer to character file GETC ; R0 gets input character LDR R1, R3, #0 ; R1 gets first character from file ; 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

Programming Exercise #1 Write a program to add the contents of R0 and R1, and indicate in R2 if there was an overflow Flow Diagram Assembly Code

Programming Exercise #1 ; Add R3=R0+R1, R2=0 indicates no overflow ; .ORIG x3000 AND R2, R2, #0 ;Initially R2=0 (no Overflow assumed) ADD R3, R0, R1 ;R3=R0+R1 ; test for overflow ADD R0, R0, #0 ;test R0 BRN NEG ;Branch if RO negative ADD R1, R1, #0 ;test R1 BRN DONE ;No overflow if operand signs differ (R1 NEG) ADD R3, R3, #0 ;maybe, test R3 BRZP DONE ;No overflow if result sign matches (All POS) ADD R2, R2, #1 ;R2=1 indicating overflow NEG ADD R1, R1, #0 ;test R1 BRZP DONE ;No overflow if operand signs differ (R1 POS) ADD R3, R3, #0 ;maybe, test R3 BRN DONE ;No overflow if result sign matches (All NEG) ADD R2, R2, #1 ;R2=1 indicating overflow DONE HALT .END

Programming Exercise #2 Write a program to count the 1’s in register R0 Flow Diagram Assembly code

Programming Exercise #2 ; Program to count 1's in Register R0 ; R3 is a working copy of R0 ; R1 contains the count ; R2 is a loop counter .orig x3100 ADD R3, R0, #0 ;copy R0 into R3 AND R1, R1, #0 ;clear count ADD R3, R3, #0 ;test for Neg BRZP NEXT ;count if Neg ADD R1, R1, #1 NEXT AND R2, R2, #0 ;check remaining 15 bits ADD R2, R2, #-15 LOOP ADD R3, R3, R3 ;shift R3 left BRZP AGAIN ;count if Neg AGAIN ADD R2, R2, #1 ;loop until done BRN LOOP HALT .END

Programming Exercise #3 Write a program to add two, two digit numbers read from the console Flow Diagram Assembly code

Programming Exercise #3 ; Program to add two 2 digit decimal numbers read from the console ; R1 & R2 are working registers to load 2 digit number ; R3 is first number ; R4 is second number ; R5 is the sum ; R6 is conversion offset .orig x3600 LEA R0, MESSAGE ;print message PUTS ; Get first number LD R0, NEWLINE ;print PROMPT1 OUT LEA R0, PROMPT1 GETC ;get first character LD R6, M30 ;convert char to hex ADD R0, R0, R6 ADD R1, R0, R0 ;R1 = 2xR0 ADD R2, R1, #0 ;copy R1 into R2 ADD R2, R2, R2 ;R2 = 4xR0 ADD R2, R2, R2 ;R2 = 8xR0 ADD R2, R2, R1 ;R2 = 10xR0 GETC ;get second character ADD R0, R0, R6 ;convert to hex ADD R3, R2, R0 ;R3 = first decimal number ; Get second number LEA R0, PROMPT2 ;get first character GETC ADD R0, R0, R6 ;convert char to hex GETC ;get second character ADD R4, R2, R0 ;R4 = first decimal number

Programming Exercise #3 (2) ; Add the numbers and print results ADD R5, R4, R3 ;R5 = R3 + R4 LEA R0, SUM ;prepare to print results PUTS LD R4, P100 ;find 1st digit LD R3, M100 AND R0, R0, #0 LOOP1 ADD R0, R0, #1 ADD R5, R5, R3 ;subtract 100 until negative BRZP LOOP1 ADD R5, R5, R4 ADD R0, R0, #-1 LD R6, P30 ;convert to ascii & print ADD R0, R0, R6 OUT AND R0, R0, #0 ;find 2nd digit LOOP2 ADD R0, R0, #1 ADD R5, R5, #-10 ;subtract 10 until negative BRZP LOOP2 ADD R5, R5, #10 ADD R0, R5, R6 ;convert and print 3rd digit LD R0, NEWLINE HALT MESSAGE .STRINGZ "Enter two 2-digit decimal numbers:" NEWLINE .FILL x000A PROMPT1 .STRINGZ " The sum of " PROMPT2 .STRINGZ " and " SUM .STRINGZ " is " M30 .FILL xFFD0 ;-x30 P30 .FILL X0030 ; x30 M100 .FILL xFF9C ;-100 P100 .FILL x0064 ; 100 .END

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 Assembly code

HW, due 11/14/07 Write and test an LC-3 assembly language program to calculate and print all the numbers in the Fibonacci series that can be stored in words of the LC-3. The program should stop when it determines it has found the largest number. Show: A snapshot of your well commented Assembly program in the LC-3 Editor Window with the Assembly response. A snapshot of the Simulator Console Display Window with the Fibonacci numbers displayed