0% found this document useful (0 votes)
19 views31 pages

Lecture 04

Uploaded by

amanmusicxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views31 pages

Lecture 04

Uploaded by

amanmusicxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CSC-430

Computer Organization &


Assembly Language

Prof. Dr. Imtiaz Ali Korejo


Email: [email protected]
Assembly Language Syntax
VARIABLES
❖Each variable has a data type
❖Each variable is assigned to a memory address by the assembler
❖Data-defining Pseudo-Ops
DB - Define a byte ; Allocates 1 byte
DW - Define a word ; Allocates 2 bytes
DD - Define a doubleword ; Allocates 4 bytes
DQ - Define a quad word ; Allocates 8 bytes
DT - Define ten bytes ; Allocates 10 bytes
❖We will use only the DB pseudo-op and the DW pseudo-op
Byte & Word Variables
ALPHA DB 4
– means define ALPHA to be a variable 1 byte in length that initially contains the value 4
ALPHA DB ?
– simply means define ALPHA to be a variable 1 byte in length but don't initialize ALPHA to
any specific value!
FIELDA DW -2
– means define FIELDA to be a variable 1 word
– (2 bytes) in length that initially contains the value -2

FIELDA DW ?
– simply means define FIELDA to be a variable 1 word (2 bytes) in length but don't initialize
FIELDA to any specific value!
Multiple initializers or Arrays
• Arrays are a sequence of memory bytes or words

• Example: an array of bytes


TABLE DB 10, 20, 30, 40 TABLE BYTE 10, 20, 30, 40
In the above example, assume TABLE is located at offset 0000. if so the value 10 is
at offset 0000, 20 is at offset 0001 and so on..
Offset Values
0000 10
0001 20
0002 30
0003 40
Multiple initializers or Arrays (cont’d)
Create an array of words by listing the elements. The following array containing a list of values:
Example:
VALUES DW 100, 400, 300, 200, 250 ; [ The legacy DW data directives]
Or mylist word 1, 2, 3, 4, 5 ; [Intrinsic Data directives ]

The following table shows of the array in memory, assuming mylist strarts at offset 0000. The
addresses increment by 2 because each value occupies
Offset Values
2 bytes.
0000 1
0002 2
0004 3
0006 4
0008 5
Multiple initializers or Arrays (cont’d)
DUP Operator:
The DUP operator allocates storage for multiple data items, using a integer
expression as a counter. It is particularly useful when allocating space for a
string or array, and can be used with initialized or uninitialized data.
Byte 20 DUP (0) ; 20 bytes, all equal to zero
Byte 20 DUP (?) ; 20 bytes, uninitialized
Byte 4 DUP (“STACK”) ; 20 bytes, “STACK STACK STACK STACK ”
smallarray dword 10 DUP(0) ; 40 bytes
bigarray dword 5000 DUP(?) ; 20,000 bytes, not initialized
Character Strings
LETTERS DB 'ABC’
– means define LETTERS to be a variable 3 bytes in length that initially contains the letters
ABC
Inside the quotes, the assembler differentiates between upper and lower case
You can combine data types when you are defining variables:
MSG DB 'ABC',61H,98,01100011B
is the equivalent of
MSG DB 'ABCabc'
Named Constants
EQU
– Assigns a name to a constant
Example:
LF EQU 0AH
– assigns the name LF to 0AH which is the ASCII code for Line Feed. The name LF can then be used as
you code your program when you want to refer to the hex value 0A
Remember, no memory is allocated for EQU names. You are simply informing
the assembler that a name refers to a specific value
Direct Memory Operands
• A direct memory operand is a named reference to storage in memory

.data
var1 BYTE 10h ;or var1 DB 10h
.code
mov al,var1 ; AL = 10h
mov al,[var1] ; AL = 10h

alternate format
MOV Instruction
• Move from source to destination. Syntax:
MOV destination,source
• No more than one memory operand permitted
• CS, EIP, and IP cannot be the destination
• No immediate to segment moves .data
count BYTE 100 ;or count DB 100
wVal WORD 2 ;or wVal DW 2
.code
mov bl,count ; mov bl,count
mov ax,wVal ; mov ax,wVal
mov count,al ; mov count,al

mov al,wVal ; error


mov ax,count ; error
mov eax,count ; error
Your turn . . .
Explain why each of the following MOV statements are invalid:

.data
bVal BYTE 100 ; or bVal DB 100
bVal2 BYTE ? ; bVal2 DB ?
wVal WORD 2 ; wVal DW 2
dVal DWORD 5 ;dVal DD 5
.code
mov ds,45 ; immediate move to DS not permitted
mov esi,wVal ; size mismatch
; EIP cannot be the destination
mov eip,dVal
;immediate value cannot be destination
mov 25,bVal
mov bVal2,bVal
MOV Instruction: Restrictions
❖The destination field can never be a constant!
❖A memory location cannot be moved to another memory location
❖The contents of a segment register cannot be moved to another segment register
❖A constant cannot be moved into a segment register

❖To move the contents of one memory location (WORD1) to another (WORD2),
try:

MOV AX,WORD1
MOV WORD2,AX
XCHG
• The XCHG instruction can be used to exchange the contents of two registers, or
to exchange the contents of a register and a memory location
• Syntax: XCHG destination, source
• Examples:
– Before the instruction executes:
AX: 1234 BX: 5678

XCHG AH,BL
swaps the high order byte of register AX with the low order byte of register BX

– After the instruction executes:


AX: 7834 BX: 5612
XCHG Instruction: Restrictions
❖Neither the destination field nor the source field can ever be a constant!

❖Neither the destination field nor the source field can ever be a segment register

❖A memory location cannot be exchanged with another memory location

❖We can exchange one memory location with another using a sequence of
exchange instructions
INC & DEC Instruction
❖Used to increment the contents of a register or a memory location by 1
❖Syntax: 1) INC destination 2) DEC Destination
❖Examples:
– Before the instruction executes: AX: 0042
– INC AX
the value in the AX register is increased by 1
– After the instruction executes: AX: 0043

Restriction: Segment registers cannot be used in both above instruction


INC and DEC Examples
.DATA
MYWORD DW 1000H .data
MYDWORD DD 1000 000H myWord WORD 1000h ;or myWord DW 1000h
myDword DWORD 10000000h ; or myWord DD 1000000
.CODE .code
INC MYWORD ;1001H inc myWord ; 1001h
DEC MYWORD ;1000H dec myWord ; 1000h
INC MYDWORD ; 1000000H inc myDword ; 10000001h

MOV AX, 00FFH mov ax,00FFh


INC AX ; AX = 0100H inc ax ; AX = 0100h
MOV AX,00FFH mov ax,00FFh
INC AL ; AL = 00H inc al ; AX = 0000h
Your turn
Show the value of the destination operand after each of the following
instructions executes:

.data
myByte BYTE 0FFh, 0 ;myByte DB 0FFh
.code
mov al,myByte ; AL = FFh
mov ah,[myByte+1] ; AH = 00h
dec ah ; AH = FFh
inc al ; AL = 00h
dec ax ; AX = FFFFh
NEG instruction
❖Reverse the sign of an operand. Operand can be a register or memory operand.
❖Syntax: NEG destination
❖Examples:
– Before the instruction executes: AX: 0001
NEG AX
the value in the AX register is negated (multiplied by -1)
– After the instruction executes: AX: FFFF
ADD &SUB Instruction
• Used to add
– the contents of two registers
– the contents of a register and a memory location
– a constant to a register
– a constant to a memory location

• Syntax: ADD destination,source


ADD & SUB Instruction: Restrictions
❖Segment registers cannot be used in an ADD instruction
❖The value in one memory location cannot be added directly to the value in
another memory location

❖To add the contents of one memory location like WORD1, to another memory
location like WORD2, try:
MOV AX, WORD1
ADD WORD2, AX
❖Note: It will destroy the original contents of AX !!
Memory Models: MODEL Directive
❖Syntax:
.MODEL memory-model

MODEL DESCRIPTION

SMALL Code in one segment, data in one segment

COMPACT Code in one segment, data in more than one segment

MEDIUM Code in more than one segment data in one segment

LARGE Code in more than one segment, data in more than


one segment, no array larger than 64K

HUGE Code in more than one segment data in more than


one segment, arrays may be larger than 64K bytes
BASIC scheme of an ALC small model program
.MODEL SMALL
.STACK 100H ; 256 BYTES FOR THE STACK AREA
.DATA
;---------------------------> data definitions go here
.CODE
MAIN PROC
;---------------------------> instructions go here
MAIN ENDP
;---------------------------> other procedures go here
END MAIN ; last instruction in
; every program
Input and Output Instructions
• The CPU communicates with peripherals through I/O ports
• Most application programs use service routines provided by the
manufacturer to do I/O
• BIOS routines
• DOS routines
• You can use the INT (interrupt instructions) to invoke specific BIOS or
DOS routines
• INT 16h invokes a BIOS routine that performs keyboard input
• INT 21h invokes a DOS routine that handles a large number of
functions
• We will study four ways to use the INT 21H instruction

23
INT 21H Instruction
❖Used to invoke a DOS routine that handles a large number of functions (See
Yu's Appendix C - pps. 456-460)
❖Syntax:
INT interrupt_number
Where interrupt_number specifies a routine.
❖A particular I/O function is requested by placing a value into AH and then
invoking the command: INT 21H
❖Output values are returned in various registers
❖The I/O functions we are interested in are:
Function No. Routine
1 Single key input
2 Single character output
9 Character String Output
4ch Return control to DOS
Function 1: Single Key Input
• Waits for a character to be read at the standard input
device, then echoes the character to the standard output
device and returns the ASCII code in AL

• INPUT: AH = 1
• OUTPUT: AL = ASCII code if character is pressed
= 0 if non-character is pressed
To read a character from the keyboard:
MOV AH,1
INT 21H

; Character is stored in AL
25
Single Character Output

AH = 2
DL = ASCII code of the display character or control character
AL = ASCII code of the display character or control character

Reading & displaying a character


To display the character ‘?’ on the screen
MOV AH,1
MOV DL, ‘?’ INT 21H
MOV AH,2
INT 21H MOV DL,AL

MOV AH,2
INT 21H
Control Characters
ASCII Code (Hex) Symbol Function
7 BEL Beep (Sound)
8 BS Backspace
9 HT Tab
A LF Line feed(new line)
D CR Carriage return (Start of current line)

27
Function 9: Display a character string
• INPUT
• AH = 9
• DX = offset address of the string to be displayed. The string must
end with a $
• OUTPUT:
• Your book says NONE
• BUT truly this instruction destroys the AL register by putting 24h in
it !!!

28
Function 4Ch: Terminate a Process (EXIT)
• Terminates a Process (EXIT)/Returns control to DOS
• INPUT
• AH = 4Ch
• AL = return code
• OUTPUT: None

29
A First Program
TITLE PGM4_1: ECHO PROGRAM
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
;display prompt
MOV AH,2 ;display character function
MOV DL,'?' ;character is '?'
INT 21H ;display it
;input a character
MOV AH,1 ;read character function
INT 21H ;character in AL
MOV BL,AL ;save it in BL
;go to a new line
MOV AH,2 ;display character function
MOV DL,0DH ;carriage return
INT 21H ;execute carriage return
MOV DL,0AH ;line feed
INT 21H ;execute line feed
;display character
MOV DL,BL ;retrieve character
INT 21H ;and display it
;return to DOS
MOV AH,4CH ;DOS exit function
INT 21H ;exit to DOS
MAIN ENDP
END MAIN
30
THE END.

You might also like