Assembly Instructions Assembly language instructions may involve mnemonics

Assembly Instructions Assembly language instructions may involve mnemonics, labels, variables, constants, and directives. Examples are as follows. here 1 msg 2 mov 5 db 6 ax, 23 "help$" ; set ax value ; ASCII bytes assigned to msg var 3 db 6 10 ; var assigned a value of 10 num 4 equ 6 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. • Value 1 dw 2 AB 6 h will place two bytes in consecutive locations with the low order byte in the lower memory location as: B 6 2 A 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 MOV MOV reg, reg mem, reg, mem, immed reg, immed • Allowable MOVs with segment registers (except CS) MOV MOV segreg, reg 16 segreg, mem 16 reg 16, segreg mem 16, segreg 7

Direct Operands 8

Illegal Moves 9

Label Directive. data count. B label byte ; byte attribute, ; no storage allocated countw dw 0020 h ; word attribute. code mov al, count. B mov cx, count. W ; retrieve low byte of count ; retrieve all of count. B count. W 20 00 10

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

Based Index Addressing Example Adding Bytes In the following example of based indexed addressing, the contents of 16 d bytes beginning at ds: 1000 h and ending with ds: 100 Fh are accumulated in the al register. cs: 100 mov al, 0 cs: 102 mov cx, 10 h cs: 105 mov si, 0 cs: 108 mov bx, 1000 h cs: 10 b add al, [bx + si] cs: 10 d inc si cs: 11 e 1 loop 10 bh ; initialize AL register ; set loop counter to 10 h=16 d ; set si=0 ; set bx=1000 h as offset address ; accum. contents of mem. loc. [bx+si] ; increment si ; return to add 12

Based Index Addressing Example Adding Words In the following example of based indexed addressing, the contents of 16 d words beginning at ds: 1000 h and ending with ds: 101 Dh are accumulated in the ax register. cs: 100 mov ax, 0 cs: 103 mov cx, 10 h cs: 106 mov si, 0 cs: 109 mov bx, 1000 h cs: 10 c add ax, [bx + si] cs: 10 e add si, 2 cs: 111 loop 10 ch ; initialize AX register ; set loop counter to 10 h=16 d ; set si=0 ; set bx=1000 h as offset address ; accum. contents of mem. loc. [bx+si] ; increment si by 2 ; 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 0000 0000 0003 0005 0007 000 A 000 C 000 F 0011 title Hello World Program (hello. asm) ; This program displays "Hello, world!" directive ->. model small <-code and data each < 64 K directive ->. stack 100 h <- 256 bytes reserved directive ->. data 48 65 6 C 6 C 6 F 2 C message db "Hello, world!", 0 dh, 0 ah, '$' 20 77 6 F 72 6 C 64 21 0 D 0 A 24 directive ->. code main proc <-begin procedure B 8 ---- R mov ax, @data <-assigns seg. Addr. 8 E D 8 mov ds, ax to DS B 4 09 mov ah, 9 BA 0000 R mov dx, offset message CD 21 int 21 h B 8 4 C 00 mov ax, 4 C 00 h CD 21 int 21 h main endp <-end procdure end main <-end assembly 16

Hello World. map File Start Stop Length Name 00000 H 00011 H _TEXT 00012 H 00021 H 00010 H _DATA 00030 H 0012 FH 00100 H STACK Origin 0001: 0 Class CODE DATA Group 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 mem 1, mem 2, but 19

Arithmetic Instructions INC and DEC Instructions inc destination dec destination ; add 1 to destination ; subtract 1 from destination where destination is reg or mem Examples: inc al dec ax dec byte ptr membyte dec memword inc word ptr memword ; dec 8 -bit memory operand ; dec memory operand ; inc 16 -bit memory operand 20

Arithmetic Instructions ADD Instruction add destination, source Example: add ax, bx Contents of Registers Before add ax, bx AX |0 FFF BX |0001 add ax, bx AX |0002 BX |FFFF After |1000 |0001 | | |0001 |FFFF | plus a | 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 |00 A 0 BX |0001 |009 F |0001 | | sub ax, bx AX |0005 BX |0006 |FFFF |0006 | | 23

Arithmetic Instructions MUL Instruction multiplier ; multiplicand in ax ; product in dx, ax Example: mul bx Contents of Registers Before After mul bx AX |FFFF BX |0010 DX |0000 |FFF 0 |0010 |000 F | | | 24

Arithmetic Instructions DIV Instruction divisor ; dividend in dx, ax: quotient in ax ; remainder in dx Example: div bx Contents of Registers Before After div bx AX |FFF 1 BX |0010 DX |000 F |FFFF |0010 |0001 | | | 25

Memory Models Produces. com files What we will use Linear addressing 26

Overlapping Segments ) 27
- Slides: 27