Lecture #10, Microprocessor-ALP-Intro
Lecture #10, Microprocessor-ALP-Intro
High-Level Language: D = A * B + 10
Operating System
Level 3
Instruction Set
Architecture Level 2
Microarchitecture Level 1
Each level hides
the details of the
Digital Logic Level 0 level below it
Programmer’s View of Computer System
❖ Application Programs (Level 5)
• Written in high-level programming languages
• Such as Java, C++, Pascal, Visual Basic . . .
• Programs compile into assembly language level (Level 4)
❖ Assembly Language (Level 4)
• Instruction mnemonics are used
• Have one-to-one correspondence to machine language
• Calls functions written at the operating system level (Level 3)
• Programs are translated into machine language (Level 2)
❖ Operating System (Level 3)
• Provides services to level 4 and 5 programs
• Translated to run at the machine instruction level (Level 2)
Programmer’s View of Computer System
❖ Instruction Set Architecture (Level 2)
• Specifies how a processor functions
• Machine instructions, registers, and memory are exposed
• Machine language is executed by Level 1 (microarchitecture)
❖ Microarchitecture (Level 1)
• Controls the execution of machine instructions (Level 2)
• Implemented by digital logic (Level 0)
❖ Digital Logic (Level 0)
• Implements the microarchitecture
• Uses digital logic gates
• Logic gates are implemented using transistors
Constants
➢ Integer Constants
• Examples: –10, 42d, 10001101b, 0FF3Ah, 777o
• Radix: b = binary, d = decimal, h = hexadecimal, and o = octal
• If no radix is given, the integer constant is decimal
• A hexadecimal beginning with a letter must have a leading 0
➢ Character and String Constants
• Enclose character or string in single or double quotes
• Examples: 'A', "d", 'ABC', "ABC", '4096'
• Embedded quotes: "single quote ' inside", 'double quote " inside'
• Each ASCII character occupies a single byte
• Example: The string constant containing “349” is 3 bytes long.
Assembly Language Statements
❖ Three types of statements in assembly language
• Typically, one statement should appear on a line
1. Executable Instructions
• Generate machine code for the processor to execute at runtime
• Instructions tell the processor what to do
2. Assembler Directives
• Provide information to the assembler while translating a program
• Used to define data, select memory model, etc.
• Non-executable: directives are not part of instruction set
3. Macros
• Shorthand notation for a group of statements
• Sequence of instructions, directives, or other macros
Instructions
• Assembly language instructions have the format:
[label:] mnemonic [operands] [;comment]
• Instruction Label (optional)
▪ Marks the address of an instruction, must have a colon :
▪ Used to transfer program execution to a labeled instruction
• Mnemonic
▪ Identifies the operation (e.g. MOV, ADD, SUB, JMP, CALL)
• Operands
▪ Specify the data required by the operation
▪ Executable instructions can have zero to three operands
▪ Operands can be registers, memory variables, or constants
Instruction Examples
✓ No operands
STC ; set carry flag
✓ One operand
INC AX ; increment register AX
JMP L1 ; jump to instruction with label L1
✓ Two operands
ADD BX, CX ; register BX = BX + CX
SUB var1, 25 ; memory variable var1 = var1 – 25
✓ Three operands
IMUL AX, BX,5 ; register AX = BX * 5
Directives
A directive is a statement that affects either the program listing or the way machine code is generated.
; Program Description:
; Author: Creation Date:
; Modified by: Modification Date:
DATA
; (insert variables here)
CODE
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
Directives
❖ TITLE line (optional)
• Contains a brief heading of the program and the disk file name
❖ DATA directive
❖ CODE directive
❖ END directive
General Form:
ProcedureName PROC [NEAR/FAR]
; save all registers that get modified using PUSH
…
; procedure codes
…
; Restore all registers that were saved in stack
RET; Return to calling program using RET instruction
ProcedureName ENDP
Procedure Call:
CALL → transfer control to subprogram or procedure.
saves return address on stack.
two types → Intra-Segment or near call.
Inter-Segment or far call.
General Form:
CALL ProcedureName
Procedure Call and Return
MAIN PROC
CALL PROC1
next instruction
PROC1 PROC
First instruction
RET
Procedures
Procedure Examples:
a) HEX2ASC PROC NEAR b) IFACT PROC FAR
… …
; procedure code ; procedure code
… …
RET RET
HEX2ASC ENDP IFACT ENDP
Invoking Macro:
MacroName Arguments
Macros
MACRO Example:
PrintString MACRO msg
mov ah, 09H ; AH=display string function
mov dx, offset msg ; DX=offset of a data item msg
int 21H ; call DOS service
ENDM
Invoking Macro:
msg1 db ‘Hello everyone!$’
PrintString msg1
After assembling→
mov ah, 09H
mov dx, offset msg1
int 21H
How Assembly Language Works
Data Defining Statement
• Sets aside storage in memory for a variable
• May optionally assign a name (label) to the data
• Syntax:
[name] directive initializer [, initializer] . . .
val dB 10
• All initializers become binary data in memory
Name
✓ The name field is used for instruction labels, procedure names, and variable names.
✓ The assembler translates names into memory addresses.
✓ Names can be from 1 to 31 characters long and may consist of letters, digits, and the
special characters ?, ., @, _, $, %.
✓ Embedded blanks are not allowed.
✓ If a period is used, it must be the first character.
✓ Names may not begin with a digit.
✓ The assembler does not differentiate between uppercase and lowercase in a name.
Name
Example:
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
This chart shows a part of the memory where these arrays are declared
You can access the value of any element in array using square brackets, for example:
MOV AL, a[3]
Array
✓ If you need to declare a large array you can use DUP operator.
The syntax for DUP:
number DUP ( value(s) )
number - number of duplicate to make (any constant value).
value - expression that DUP will duplicate.
Example:
1. c DB 5 DUP(9)
is an alternative way of declaring:
c DB 9, 9, 9, 9, 9
2. d DB 5 DUP(1, 2)
is an alternative way of declaring:
d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
Defining Strings
• A string is implemented as an array of characters
• For convenience, it is usually enclosed in quotation marks
• It is often terminated with a NULL char (byte value = 0)
• Examples:
calc:
ADD AX, BX ; add BX to AX.
JMP back ; go 'back’.
stop:
EQU Directive
• Three Formats:
Name EQU Expression Integer constant expression
Name EQU Symbol Existing symbol name
Name EQU <text> Any text may appear within < …>