0% found this document useful (0 votes)
7 views

AL Basics

Uploaded by

Mah Rukh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AL Basics

Uploaded by

Mah Rukh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Assembly Instructions

Assembly language instructions may involve mnemonics, labels,


variables, constants, and directives. Examples are as follows.

here1 mov5 ax,23 ;set ax value


msg2 db6 "help$" ;ASCII bytes assigned to msg
var3 db6 10 ;var assigned a value of 10
num4 equ6 20 ;num has the value 20
_____________________________________________________________
1
label 4 constant
2
variable of ASCII bytes 5
mnemonic
3
one-byte numeric variable 6
directive

1
Define Byte, Word

Define Byte Variables (memory locations)

Define Word Variables (memory locations)

2
Define Byte, Word

• list db 10,20,30,40
will store the four values in consecutive locations. If the
offset of list is 0000, they will be stored in 0000, 0001,
0002, 0003, respectively. list refers to 0000, list+1
refers to 0001, etc.

• Value1 dw 2AB6h
will place two bytes in consecutive locations with the low
order byte in the lower memory location as: B6 2A

3
Character or String Constants

• ‘ABC’
• “This is a test.”
• ‘This is my file.’

• Define a variable called message stored


at offset 0000 for any of the above.
For example: message db ‘ABC’.

• Then “B” of “ABC” is at offset 0001 and


“i” of “This …” is at offset 2

4
Pointer

Given:
message db ‘ABC’ ;define 3 bytes
P db message ;P points to
message

The variable P contains the offset of


message

5
ASCII Table

6
MOV Instruction
• Allowable MOVs with scratch registers
MOV reg,reg
MOV mem,reg
MOV reg,mem
MOV mem,immed
MOV reg,immed

• Allowable MOVs with segment registers (except CS)


MOV segreg,reg16
MOV segreg,mem16
MOV reg16,segreg
MOV mem16,segreg

7
Direct Operands

8
Illegal Moves

9
Label Directive

.data
countB label byte ;byte attribute,
;no storage allocated
countw dw 0020h ;word attribute

.code
mov al,countB ;retrieve low byte of count
mov cx,countW ;retrieve all of count
countB
countW
20 00
Addressing Modes

Addressing Mode Example Description


Direct mov ax,bx Effective address (EA) are
the registers
Register Indirect mov ax,[bx] EA is the contents of a register
Based mov ax,[bp + 1] EA is the sum of a base register
and a displacement
Indexed mov [di + 2],ax EA is the sum of an index
register and a displacement
Based Indexed mov ax,[bx + si] EA is the sum of a base register
and an index register
Based Indexed with mov ax,[bx + si + 2] EA is the sum of a base
Displacement register, an index register and a
displacement

11
Based Index Addressing Example
Adding Bytes

In the following example of based indexed addressing, the contents of


16d bytes beginning at ds:1000h and ending with ds:100Fh are
accumulated in the al register.

cs:100 mov al,0 ;initialize AL register


cs:102 mov cx,10h ;set loop counter to 10h=16d
cs:105 mov si,0 ;set si=0
cs:108 mov bx,1000h ;set bx=1000h as offset address
cs:10b add al,[bx + si] ;accum. contents of mem. loc. [bx+si]
cs:10d inc si ;increment si
cs:11e1 loop 10bh ;return to add

12
Based Index Addressing Example
Adding Words

In the following example of based indexed addressing, the contents of


16d words beginning at ds:1000h and ending with ds:101Dh are
accumulated in the ax register.

cs:100 mov ax,0 ;initialize AX register


cs:103 mov cx,10h ;set loop counter to 10h=16d
cs:106 mov si,0 ;set si=0
cs:109 mov bx,1000h ;set bx=1000h as offset address
cs:10c add ax,[bx + si] ;accum. contents of mem. loc. [bx+si]
cs:10e add si,2 ;increment si by 2
cs:111 loop 10ch ;return to add

13
Stack Operation

To save register contents before calling a subroutine:

;save register contents before calling subroutine


push ax
push bx
push cx
push dx
;restore register contents after returning from subroutine
pop dx
pop cx
pop bx
pop ax

14
Assemble-Link-Execute Cycle

.asm MASM .exe

.obj

.lst
.map

15
Hello World .lst File

title Hello World Program (hello.asm)


;This program displays "Hello, world!"
directive ->.model small <-code and data each < 64K
directive -> .stack 100h <- 256 bytes reserved
0000 directive -> .data
0000 48 65 6C 6C 6F 2C message db "Hello, world!",0dh,0ah,'$'
20 77 6F 72 6C 64
21 0D 0A 24
0000 directive ->.code
0000 main proc <-begin procedure
0000 B8 ---- R mov ax,@data <-assigns seg. Addr.
0003 8E D8 mov ds,ax to DS
0005 B4 09 mov ah,9
0007 BA 0000 R mov dx,offset message
000A CD 21 int 21h
000C B8 4C00 mov ax,4C00h
000F CD 21 int 21h
0011 main endp <-end procdure
end main <-end assembly
16
Hello World .map File

Start Stop Length Name Class


00000H 00010H 00011H _TEXT CODE
00012H 00021H 00010H _DATA DATA
00030H 0012FH 00100H STACK
STACK

Origin Group
0001:0 DGROUP

Program entry point at 0000:0000

17
XCHG Instruction

Problem: move bx to ax and ax to bx

mov cx,ax ;ax stored temporarily in cx


mov ax,bx ;move bx to ax
mov bx,cx ;move cx (really ax) to bx
or use:
xchg ax,bx
Allowed:
xchg reg,reg
xchg reg,mem
xchg mem,reg

18
XCHGing Two Variables

Cannot do: xchg mem1,mem2, but

19
Arithmetic Instructions
INC and DEC Instructions
inc destination ;add 1 to destination
dec destination ;subtract 1 from destination

where destination is reg or mem

Examples:
inc al
dec ax
dec byte ptr membyte ;dec 8-bit memory operand
dec memword ;dec memory operand
inc word ptr memword ;inc 16-bit memory operand

20
Arithmetic Instructions
ADD Instruction
add destination, source

Example: add ax,bx

Contents of Registers
Before After

add ax,bx AX |0FFF |1000 |


BX |0001 |0001 |

add ax,bx AX |0002 |0001 | plus a


BX |FFFF |FFFF | carry

21
Arithmetic Instructions
ADD Instruction

Consider the way in which add and adc, add with a carry,
deal differently with the carry flag. Both the AX and BX
registers contain 0000 and the carry flag is set, CY.

add ax,bx yields AX=0, BX=0 and NC (no carry)

adc ax,bx yields AX=1, BX=0 and NC

22
Arithmetic Instructions
SUB Instruction
sub destination, source

Example: sub ax,bx

Contents of Registers
Before After

sub ax,bx AX |00A0 |009F |


BX |0001 |0001 |

sub ax,bx AX |0005 |FFFF |


BX |0006 |0006 |

23
Arithmetic Instructions
MUL Instruction
mul multiplier ;multiplicand in ax
;product in dx,ax

Example: mul bx

Contents of Registers
Before After

mul bx AX |FFFF |FFF0 |


BX |0010 |0010 |
DX |0000 |000F |

24
Arithmetic Instructions
DIV Instruction
div divisor ;dividend in dx,ax: quotient in ax
;remainder in dx

Example: div bx

Contents of Registers
Before After

div bx AX |FFF1 |FFFF |


BX |0010 |0010 |
DX |000F |0001 |

25
Memory Models

Produces .com
files
What we will
use

Linear
addressin
g 26
Overlapping Segments

27

You might also like