Human Readable Language-Assembly Language Programming-Lecture Slides, Slides of Assembly Language Programming

This lecture was delivered by Mr. Gurpreet Verma at Cochin University of Science and Technology for Assembly Language Programming course. It includes: Assembler, Correspondence, Symbols, Loop, Syntax, Prompt, Character, Occurrences, Match, Halt, Convert

Typology: Slides

2011/2012

Uploaded on 07/26/2012

parinita
parinita 🇮🇳

4.8

(6)

67 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 7
ASSEMBLY LANGUAGE
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Human Readable Language-Assembly Language Programming-Lecture Slides and more Slides Assembly Language Programming in PDF only on Docsity!

CHAPTER 7

ASSEMBLY LANGUAGE

HUMAN-READABLE MACHINE LANGUAGE

Computers like ones and zeros…

Humans like symbols…

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

ADD R6,R2,R6 ; increment index reg.

0001110010000110

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

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)

 label -- symbolic name of memory location

 separated by comma

 number, order, and type correspond to instruction format

 ex: ADD R1,R1,R ADD R1,R1,# LD R6,NUMBER BRz LOOP

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 (^) address starting address of program

.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)

Ptr = 1st file character (R3 = M[x3012])

Input char from keybd (TRAP x23)

Done? (R1 ?= EOT)

Load char from file (R1 = M[R3])

Match? (R1 ?= R0)

Incr Count (R2 = R2 + 1)

Load next char from file (R3 = R3 + 1, R1 = M[R3])

Convert count to ASCII character (R0 = x30, R0 = R2 + R0)

Print count (TRAP x21)

HALT (TRAP x25)

NO

NO

YES

YES

CHAR COUNT IN ASSEMBLY LANGUAGE (1 OF 3)

; ; 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 x AND R2, R2, #0 ; R2 is counter, initially 0 LD R3, PTR ; R3 is pointer to characters GETC ; R0 gets character input LDR R1, R3, #0 ; R1 gets first character ; ; Test character for end of file ; TEST ADD R4, R1, #-4 ; Test for EOT (ASCII x04) BRz OUTPUT ; If done, prepare the output (^) 7-

CHAR COUNT IN ASSEMBLY LANGUAGE (3 OF 3)

; ; Storage for pointer and ASCII template ; ASCII .FILL x PTR .FILL x .END

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;

this is called the symbol table

Second Pass:

 convert instructions to machine language,

using information from symbol table

PRACTICE

Construct the symbol table for the program in Figure 7.

(Slides 7-11 through 7-13).

Symbol Address

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,# ADD R1,R ADD R3,R3,NUMBER

 Immediate argument too large

 ex: ADD R1,R2,#

 Address (associated with label) more than 256 from instruction

 can’t use PC-relative addressing mode

LC-3 ASSEMBLER

Using “assemble” (Unix) or LC3Edit (Windows),

generates several different output files.

This one gets loaded into the simulator.

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:

0011000000000000

0101010010100000

0010011000010001

1111000000100011

.

.

.

.ORIG x

AND R2, R2, #

LD R3, PTR

TRAP x