Sie sind auf Seite 1von 20

Computer Organization & Assembly Language

Assembly Language Fundamentals

Outline

Assembly Language Basic Elements Sample Hello Program

Basic Elements of Assembly Language

Constants and Expressions


Numeric Literals Character or String constant Name Field Operation Field Operands(s) Field Comment Field Labels for Variables Code Labels
Irvine: Assembly Language for Intel-Based Computers (1999)

Statements

Names

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 0BAF1h ; hexadecimal ; leading zero required

32q
1011b 35d

; octal
; binary ; 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 COUNT MOD 5 ; (ROWS is a constant)

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

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

10

Contd..

Operation Field: Symbolic operation (Op code)

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

11

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 Optional Marked by semicolon in the beginning Ignored by assembler Good practice

Comments Field

12

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! .model small .stack 100h comment line 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 General Register Source Operand General Register Legal YES

General Register
Memory Location Memory Location

Memory Location
General Register Memory Location

YES
YES NO

Legal Combinations of Operands for MOV


Destination Operand General Register General Register General Register General Register Memory Location Memory Location Memory Location Memory Location Source Operand General Register Memory Location Segment Register Constant General Register Memory Location Segment Register Constant Legal YES YES YES YES YES NO YES YES

Legal Combinations of Operands for MOV


Destination Operand Segment Register Segment Register Segment Register Segment Register Constant Constant Constant Constant Source Operand General Register Memory Location Segment Register Constant General Register Memory Location Segment Register Constant Legal YES YES NO NO NO NO NO NO

Legal Combinations of Operands for ADD & SUB


Destination Operand Source Operand Legal

General Register
General Register General Register

General Register
Memory Location Constant

YES
YES YES

Memory Location
Memory Location Memory Location

General Register
Memory Location Constant

YES
NO 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