Download Slide 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Organization &
Assembly Language

Assembly Language Fundamentals
Outline
Assembly Language – Basic Elements
Sample Hello Program


2
Basic Elements of Assembly Language

Constants and Expressions



Statements





Numeric Literals
Character or String constant
Name Field
Operation Field
Operands(s) Field
Comment Field
Names


Labels for Variables
Code Labels
Irvine: Assembly Language for Intel-Based Computers (1999)
Numeric Literals

A numeric literal is any combination of digits plus:


optional decimal point, exponent, sign
Examples of decimal literals:
5234
5.5
-5.5
26.0E+05
+35d
Irvine: Assembly Language for Intel-Based Computers (1999)
Contd..

Use a radix symbol (suffix) to select binary, octal,
decimal, or hexadecimal
6A15h
; hexadecimal
0BAF1h
; leading zero required
32q
; octal
1011b
; binary
35d
; decimal (default)
Irvine: Assembly Language for Intel-Based Computers (1999)
Symbolic Constant (Integer)


Defined using the = operator
Must evaluate to a 16-bit integer

or 32-bit integer when .386 directive used
COUNT = 25
ROWS = 10
tablePos = ROWS * 5
Irvine: Assembly Language for Intel-Based Computers (1999)
Constant Expression

Combination of numeric literals, operators, and
defined symbolic constants


must be evaluated at assembly time
Examples:
4 * 20
-3 * 4 / 6
ROWS - 3
; (ROWS is a constant)
COUNT MOD 5
Irvine: Assembly Language for Intel-Based Computers (1999)
Statements



Syntax:
name operation operand(s) comments
 name and comment are optional
 Number of operands depend on the instruction
One statement per line
Each statement is either:
Instruction (translated into machine code)
 Assembler Directive (instructs the assembler to perform some specific
task such as allocating memory space for a variable or creating a
procedure)
At least one blank or tab character must separate the field.


Statement Example
mnemonic
Operands
Comment
Label
; Code
Here: mov ax,count
; store count into ax
; Data, using DW directive
myArray dw 1000h,2000h
Data name
dw 3000h,4000h
Main PROC
Label
Operation
Irvine: Assembly Language for Intel-Based Computers (1999)
Contd..

Name Field





10
The assembler translates names into memory addresses.
Names can be 1 to 31 character long and may consist of letter,
digit or special characters. If period is used, it must be first
character.
Embedded blanks are not allowed.
May not begin with a digit.
Not case sensitive
Contd..

Operation Field: Symbolic operation (Op code)



11
Symbolic opcode translated into ML opcode
In an assembler directive, the operation field represents
Pseudo-opcode
Pseudo-op is not translated into ML opcode, it only tells
assembler to do something. Example: PROC psuedo-op is used
to create a procedure
Contd..

Operand Field




An instruction may have zero, one or more operands.
In two-operand instruction, first operand is destination, second
operand is source.
For an assembler directive, operand field represents more
information about the directive
Comments Field




12
Optional
Marked by semicolon in the beginning
Ignored by assembler
Good practice
The Hello World Program
title Hello World Program
(hello.asm)
; This program displays “Hello, world!”
.model small
.stack 100h
.data
message db “Hello, world!”,0dh,0ah,’$’
.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
mov ax,4C00h
int 21h
main endp
end main
Irvine: Assembly Language for Intel-Based Computers (1999)
Sample Hello Program
program title (comment)
title Hello World Program
(hello.asm)
; This program displays “Hello, world!”
comment line
.model small
.stack 100h
memory model
set the stack size
Irvine: Assembly Language for Intel-Based Computers (1999)
Sample Hello Program
starts the data segment
.data
message db “Hello, world!”,0dh,0ah,’$’
starts the code segment
.code
main proc
mov ax,@data
sets DS to the offset of the
mov ds,ax
data segment
mov ah,9
mov dx,offset message
int 21h
calls DOS display function 9
mov ax,4C00h
int 21h
main endp
end main
halts program
Irvine: Assembly Language for Intel-Based Computers (1999)
Legal Combinations of Operands for
XCHG
Destination Operand
Source Operand
Legal
General Register
General Register
YES
General Register
Memory Location
YES
Memory Location
General Register
YES
Memory Location
Memory Location
NO
Legal Combinations of Operands for
MOV
Destination Operand
Source Operand
Legal
General Register
General Register
YES
General Register
Memory Location
YES
General Register
Segment Register
YES
General Register
Constant
YES
Memory Location
General Register
YES
Memory Location
Memory Location
NO
Memory Location
Segment Register
YES
Memory Location
Constant
YES
Legal Combinations of Operands for
MOV
Destination Operand
Source Operand
Legal
Segment Register
General Register
YES
Segment Register
Memory Location
YES
Segment Register
Segment Register
NO
Segment Register
Constant
NO
Constant
General Register
NO
Constant
Memory Location
NO
Constant
Segment Register
NO
Constant
Constant
NO
Legal Combinations of Operands for
ADD & SUB
Destination Operand
Source Operand
Legal
General Register
General Register
YES
General Register
Memory Location
YES
General Register
Constant
YES
Memory Location
General Register
YES
Memory Location
Memory Location
NO
Memory Location
Constant
YES
References

Chapter 3, Ytha Yu and Charles Marut, “Assembly Language Programming and
Organization of IBM PC”

Chapter 3,Assembly Language for Intel Based-Computers
20