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

Slides:



Advertisements
Similar presentations
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Advertisements

PART 6 Programming 1.Decomposition 2.Three Constructs 3.Counting Example 4.Debugging.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language.
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.
LC-3 Assembly Language Programming Examples
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 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.
Chap 4 & 5 LC-3 Computer LC-3 Instructions Chap 4 Homework – due Monday October 27 Chap 5 Homework – due Wednesday October 29 Project 2 Designs (Working.
Overview Intro to Project 2 - Serial I/O – RS232, USB Assembly Language Programming Using the LC-3 Simulator.
Introduction to LC-3 Assembly Language
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.
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.
Chapter 7 LC-3 Assembly Language. 2 College of Charleston, School of Science and Mathematics Dr. Anderson, Computer Science CS 250 Comp. Org. & Assembly.
Assembly Language.
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.
Computer Science 210 Computer Organization Overview of Assembly Language.
Chapter 7 Assembly Language. 7-2 Human-Readable Machine Language Computers like ones and zeros… Humans like symbols… Assembler is a program that turns.
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
Computer Science 210 Computer Organization
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
COSC121: Computer Systems
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
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
Computer Science 210 Computer Organization
Chapter 7 Assembly Language
Chapter 9 TRAP Routines and Subroutines
Chapter 7 LC-2 Assembly Language.
COSC121: Computer Systems
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 7 Assembly Language An Hong 2016 Fall
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 9 TRAP Routines and Subroutines
Presentation transcript:

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

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 optionalmandatory Example: Loop1 ADD R3, R3, #-1 ; Decrement R3

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

Compute the Sum of 12 Integers Program begins at location x3000. Integers begin at location x3100. R1  x3100 R3  0 (Sum) R2  12(count) R2=0? R4  M[R1] R3  R3+R4 R1  R1+1 R2  R2-1 NO YES R1: “Array” index pointer (Begin with location 3100) R3: Accumulator for the sum of integers R2: Loop counter (Count down from 12) R4: Temporary register to store next integer

Compute the Sum of 12 Integers AddressInstructionComments x R1  x3100 x R3  0 x R2  0 x R2  12 x If Z, goto x300A x Load next value to R4 x Add to R3 x Increment R1 (pointer) X Decrement R2 (counter) x Goto x3004 R1: “Array” index pointer (Begin with location 3100) R3: Accumulator for the sum of integers R2: Loop counter (Count down from 12) R4: Temporary register to store next integer

Compute the Sum of 12 Integers - Program.ORIG x3000 ; Add 12 integers ; R1: Pointer to integer ; R2: Loop counter ; R3: Accumulator ; R4: Temporary register LD R1 DATAADDR ; Load pointer ; to integers AND R3, R3, #0 ; Accumulator = 0 AND R2, R2, #0 ; Counter = 12 ADD R2, R2, #12 ; Add integers LOOP BRZ STOP ; Stop when done LDR R4, R1, #0 ; Add next integer ADD R3, R3, R4 ADD R1, R1, #1 ; Inc pointer ADD R2, R2, #-1 ; Dec counter BRNZP LOOP STOP BRNZP STOP ; Stop DATAADDR.FILL x3100.END Note: Used DATAADDR to hold address of DATA. Why? R1  x3100 R3  0 (Sum) R2  12(count) R2=0? R4  M[R1] R3  R3+R4 R1  R1+1 R2  R2-1 NO YES

Compute the Sum of 12 Integers - Data.ORIG x3100 ; Data section DATA.FILL x0001 ; 12 integers.FILL x0002.FILL x0004.FILL x0008.FILL xFFFF.FILL xFFFE.FILL xFFFC.FILL xFFF8.FILL x0007.FILL x0004.FILL x0002.FILL x0003.END

Compute the Sum of 12 Integers Use the LC3 Editor to enter the program and data and store them as add1.asm data1.asm Use the LC3 Editor to assemble them: add1.asm  add1.obj data1.asm  data1.obj Then use the LC3 Simulator to test them: load add1.obj and data1.obj Set the PC, appropriate breakpoints, and execute the program (single step or run)

Sum of 12 Integers in One Package.ORIG x3000 ; Add 12 integers ; R1: Pointer to integer ; R2: Loop counter ; R3: Accumulator ; R4: Temporary register LEA R1, DATA ; Load pointer to integers AND R3, R3, #0 ; Accumulator = 0 AND R2, R2, #0 ; Counter = 12 ADD R2, R2, #12 LOOP BRZ STOP ; Stop when done LDR R4, R1, #0 ; Add next integer ADD R3, R3, R4 ADD R1, R1, #1 ; Inc pointer ADD R2, R2, #-1 ; Dec counter BRNZP LOOP STOP BRNZP STOP ; Stop ; Data section DATA.FILL x0001 ; 12 integers.FILL x0002.FILL x0004.FILL x0008.FILL xFFFF.FILL xFFFE.FILL xFFFC.FILL xFFF8.FILL x0007.FILL x0004.FILL x0002.FILL x0003.END Note: Used LEA to load pointer to data. Why?

One Pass vs Two Pass Assemblers What does the assembler need to do? –Check for syntax errors –Build a symbol table –Assemble statements –Use pseudo-op instructions –Resolve Addresses –Create a load module Two Pass – Checks for syntax errors and builds the Symbol Table during first pass, resolves operand addresses during second pass. One Pass – Checks for syntax errors, builds the Symbol Table, and resolves operand addresses during the first pass. So why have a two pass?

An Assembly Language Program Example ; ; Program to multiply a number by the constant 6 ;.ORIGx3050 LDR1, SIX ; Constant 6 LDR2, NUMBER ; Number ANDR3, R3, #0 ; Clear R3 ; The product. ; The multiply loop AGAINADDR3, R3, R2 ; Accumulate product ADDR1, R1, #-1 ; Dec counter BRpAGAIN HALT NUMBER.BLKW3 ; Value of Number SIX.FILLx0006.END Symbol Table: Symbol Address AGAIN x3053 NUMBER x3057 SIX x305A clear R3 add R3 to R2 decrement R1 R1 = 0? HALT No Yes

What if there were More than One Object (Load) File Example Symbol Table: Symbols Externals Exports Addresses Start x3000 Number x300A Data ? Value x30E0 The “Linker/Loader” would generate another “global symbol table” to resolve Externals & Exports at Load time. It would only address the Externals (Imports) and Exports (Internals). An Export is a value make available to other modules An External is a value expected to be defined in another module

Trap Codes LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember their TRAP #’s. CodeEquivalentDescription HALTTRAP x25Halt execution and print message to console. INTRAP x23Print prompt on console, read (and echo) one character from keyboard. Character stored in R0[7:0]. OUTTRAP x21Write one character (in R0[7:0]) to console. GETCTRAP x20Read one character from keyboard. Character stored in R0[7:0]. PUTSTRAP x22Write null-terminated string to console. Address of string is in R0.

Program to add two single digit integers.ORIG x3000; begin at x3000 ; input two numbers IN ;input an integer character (ascii) {TRAP 23} LD R3, HEXN30;subtract x30 to get integer ADD R0, R0, R3 ADD R1, R0, #0 ;move the first integer to register 1 IN ;input another integer {TRAP 23} ADD R0, R0, R3;convert it to an integer ; add the numbers ADD R2, R0, R1 ;add the two integers ; print the results LEA R0, MESG ;load the address of the message string PUTS ;"PUTS" outputs a string {TRAP 22} ADD R0, R2, #0 ;move the sum to R0, to be output LD R3, HEX30;add 30 to integer to get integer character ADD R0, R0, R3 OUT ;display the sum {TRAP 21} ; stop HALT ;{TRAP 25} ; data MESG.STRINGZ"The sum of those two numbers is: “ HEXN30.FILLxFFD0 ; -30 HEX HEX30.FILLx0030 ; 30 HEX.END

Program to add two single digit integers Enter, Assemble, Load Execute, and Test the program.

Write a program to count the 1’s in register R0 ; Program to count 1's in Register R0 ; R3 is a working copy of R0 ; R1 contains the count ; R2 is a loop counter.origx3100 ADD R3, R0, #0;copy R0 into R3 AND R1, R1, #0;clear count ADD R3, R3, #0;test highest bit BRZP NEXT;count if neg ADD R1, R1, #1 NEXTAND R2, R2, #0 ;check remaining 15 bits ADD R2, R2, #-15 ; R2 = -15 (count) LOOPADD R3, R3, R3;shift R3 left BRZP AGAIN ADD R1, R1, #1 ;count if neg AGAINADD R2, R2, #1 ;inc count BRN LOOP HALT.END

Program to Check for overflow ; 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;R0 pos, test R1 BRN DONE;R0 pos, R1 neg -> No overflow ADD R3, R3, #0 ;R0 pos, R1 pos, maybe, test R3 BRZP DONE ;R3 also pos -> no overflow ADD R2, R2, #1;Set R2=1 indicating overflow BRNZP DONE R0NEG ADD R1, R1, #0;R0 neg, test R1 BRZP DONE;R0 neg, R1 pos -> No overflow ADD R3, R3, #0;R0 neg, R1 neg, maybe, test R3 BRN DONE;R3 also neg -> no overflow ADD R2, R2, #1 ;Set R2=1 indicating overflow DONE HALT.END

Count the occurrences of a character in a file.

; 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 ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to character file GETC; R0 gets input character LDRR1, R3, #0; R1 gets first character from file ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Covert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030; ASCII offset PTR.FILLx4000; PTR to character file.END