Post on 15-Jan-2016
CEN 226: Computer Organization & Assembly Language :CSC 225(Lec#8)
ByDr. Syed Noman
2
Assembly Language Statements
Generally fall into two classes:
Instructions - Executable Statements
Directives – statements that provide information to the assembler about how to generate executable code
3
Names (Identifiers)
•Used to identify variables, constants, procedures, or code labels.▫Max 247 characters ▫Not case-sensitive ▫First character can be a letter, ‘_’, ‘$’, ‘@’▫Subsequent characters may also be digits.▫Cannot use an assembler reserved word.
•Should make identifier names descriptive and easy to understand
4
Standard Assembler Directives
Directive Description
title Title of the listing file
.model Specify the program's memory model
.stack Set the size of the stack segment
.data Mark the beginning of the data segment
.code Mark the beginning of the code segment
proc Begin procedure
endp End of procedure
end End of program assembly
5
Data Allocation DirectivesDirective Description Bytes
DB Define byte 1DW Define word 2DD Define Doubleword 4
DF,DP Define far pointer 6DQ Define quadword 8DT Define tenbytes 10
Char1 db ‘A’ hex db 41hSigned1 db -128 dec db 65Signed2 db +127 bin db 01000001bUnsigned db 255 hex2 db 0A3h
6
Addressing Modes
•The 80x86 processors let you access memory in many different ways.
•The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access variables, arrays, records, pointers, and other complex data types.
7
AL 00Immediate Addressing Mode
Mov AL, 3CHAL 3C
8
Register Addressing Mode
Mov AL, BL
AL 00BL 4D
AL 4DBL 4D
9
Direct Addressing Mode
0023 12
0022 34
0021 56
0020 78
Mov CL, [0020H]
Mov CX, [0020H]
Memory (data)
Little Endian Format – The “little” end of the number is stored first.
CL 78
CX 5678
Mov ECX, [0020H]ECX
12345678
10
0022
Register Indirect Addressing Mode
location
0023 12
0022 34
0021 56
0020 78Mov CL, [SI]
In the 8086, only BX, BP, SI and DI may be used as memory pointers. Later processors don’t have this restriction.
Mov SI, 0022H
CL 34
SI
11
BX 0020
Base + Displacementlocation
0023 12
0022 34
0021 56
0020 78Mov AL, [BX +2]
Useful when accessing individual elements in an array. Note that the array begins with element 0, element 2 corresponds to the third location in the array. The displacement corresponds to byte offset from the base, not element number in the array.
Mov BX, 0020H
AL 34
12
BX 0020
Base + Index + Displacement(Useful when accessing individual elements in an record)
Mov BX, 0020H ;BX points to record starting address
SI 000C
Mov SI, 000CH ;SI points to record three (4 elements ;per record x 3 records = 000C)
Mov AL, [BX+SI+1] ;AL now has the data in element 1 of ;record #3 (assumes elements are 1 byte)
13
0006 560005 780004 120003 340002 790001 630000 35
memory (data)
Direct Addressing Direct-Offset Addressing
Offset operator – returns the 16-bit address of the variable. Good for strings and arrays.
.dataBytelist db 35h, 63h , 79hWordlist dw 1234h, 5678h.code…
Mov al, Bytelist ; al = 35Mov al, Bytelist+1 ; al = 63Mov bx, Wordlist ; bx = 1234Mov bx, Wordlist+2 ; bx = 5678
14
0006 470005 4E0004 490003 520002 540001 530000 41
memory (data)
Direct-Offset Addressing with Strings.dataaString db “ASTRING”
.code…Mov al, aString ; al = 41Mov al, aString+1 ; al = 53Mov al, aString+2 ; al = 54Mov al, aString+3 ; al = 52
15
• Base + Displacement▫Mov AL, [BX + 4]
• Base + Index + Displacement▫Mov AL, [BX+SI+3]
• Direct-Offset Addressing▫Mov al, Bytelist+1
• Immediate▫ Mov AL, 4CH
• Register▫ Mov AL, BL
• Direct▫ Mov AL, [20H]
• Register Indirect▫ Mov AL, [SI]
Instruction Addressing Modes
16
Displaying A String Which May Contain A $-Character (Dos Function 40h)
.DATA
. . . STRING_NAME DB ‘STRING THAT CONTAINS $ IS DISPLAYED’ STRINGLEN EQU $ - STRING_NAME
; $ refers to the current value in the location counter
. . .
.CODE
. . . MOV AH , 40H MOV BX , 01H ; file handle for the screen MOV CX , STRINGLEN ; string length MOV DX , OFFSET STRING_NAME INT 21H . . .
17
EP: Displaying $.model small.stack 100h.data$msg1 db " characters: $"@msg2 EQU $ - $msg1
.codestart:mov ax, @data mov ds, ax
mov ah,40hmov bx,1mov cx,@msg2mov dx, offset $msg1 int 21h
mov ax,4c00hint 21hend
18
BUFFERED KEYBOARD INPUT • To read a string, a buffer (i.e., an array) must be
defined to store that string. One way of defining the buffer is:
• BUFFER_NAME DB Num1 , Num2 DUP(?)▫Num1 is the maximum number of string characters to
be read including the Carriage Return (0Dh), which is stored as the last character.
▫Num2 has a value which is one more than Num1; it is reserved to hold the actual length of the string (i.e., minus the Carriage Return character), the string, and the terminating Carriage Return character.
Note: The maximum value for Num1 is FEh i.e., 254
19
BUFFERED KEYBOARD INPUT
20
BUFFERED KEYBOARD INPUT BUFFERED KEYBOARD INPUT (READING A STRING)(DOS FUNCTION 0AH): ANOTHER WAY OF DEFINING THE BUFFER To read a string of , say, maximum length 50 the buffer can be defined as:
MAXLEN DB 51ACTLEN DB ?BUFFER DB 51 DUP(?)
To read a string into the buffer, we invoke DOS function 0AH as:
MOV AH , 0AHMOV DX , OFFSET MAXLENINT 21H
21
BUFFERED KEYBOARD INPUT Displaying a buffer, defined by using DOS function 40H: MOV AH , 40HMOV BX , 01H ; file handle for the screenMOV CH , 00H ; Initialize CX with the string lengthMOV CL , BUFFER[1] ;LEA DX , BUFFER[2] ; Output the string starting at byte 2INT 21H ; Note: The statement:MOV DX , OFFSET BUFFER[2] is equivalent to:LEA DX , BUFFER[2]
22
BUFFERED KEYBOARD INPUT BUFFERED KEYBOARD INPUT (READING A STRING)(DOS FUNCTION 0AH): ANOTHER WAY OF DEFINING THE BUFFER To read a string of , say, maximum length 50 the buffer can be defined as:
MAXLEN DB 51ACTLEN DB ?BUFFER DB 51 DUP(?) ; becoz consecutive bytes
To read a string into the buffer, we invoke DOS function 0AH as:
MOV AH , 0AHMOV DX , OFFSET MAXLENINT 21H
23
BUFFERED KEYBOARD INPUT
Displaying a buffer, defined in by using DOS function 40H:
MOV AH , 40HMOV BX , 01H ;file handle for the screen
MOV CH , 00H ; Initialize CX with the string length
MOV CL , ACTLENLEA DX , BUFFER ; Display the buffer
INT 21H
24
Practice program
•Write a program in assembly language that inputs and prints a string.▫Using count stored in second byte▫Using loop and stop when found enter.