Professional Documents
Culture Documents
Instructions (1)
Review
Data entering
MASM
Directives
Outline
Data transfer operations Arithmetic operations Logic operation Control operations String operations
SEGMENT BYTE 'DATA' ;Define the data segment EQU 9 ;define a constant via EQU DB 'Hello World',13,10,'$' ;Define the character string ENDS
SEGMENT BYTE 'CODE' ;Define mov ax, SEG hellodat mov ds, ax mov ah, dos_print mov dx,OFFSET strng int 21h mov ax, 4c00h int 21h ENDS END START the Code segment ;ax <-- data segment start address ;ds <-- initialize data segment register ;ah <-- 9 DOS 21h string function ;dx <-- beginning of string ;DOS service interrupt ;ax <-- 4c DOS 21h program halt function ;DOS service interrupt
hellocod
.code START: mov mov mov mov int mov int ax, SEG strng ds, ax ah, dos_print dx,OFFSET strng 21h ax, 4c00h 21h ;ax <-- data segment start address ;ds <-- initialize data segment register ;ah <-- 9 DOS 21h string function ;dx <-- beginning of string ;DOS service interrupt ;ax <-- 4c DOS 21h program halt function ;DOS service interrupt
END
START
end of program, label is entry point begin a procedure; far, near keywords specify if procedure in different code segment (far), or same code segment (near) end of procedure set a page format for the listing file title of the listing file mark start of code segment mark start of data segment set size of stack segment
maxint equ
count equ
32767
10 * 20 ; calculate a value (200)
Destination Source Memory Accumulator Accumulator Memory Register Register Register Memory Memory Register Register Immediate Memory Immediate Seg. Reg. Register Seg. Reg. Memory Register Seg. Reg. Memory Seg. Reg.
Arithmetic
Logic Instructions
Bit Level Word Level Computes Results AND Modifies Status Flags
bx bx
bx
cx cx cl cx
ax, ax, ax
bx bx
;axax+bx and set flags ;axax+bx+CF(lsb) and set flags ;axax+1 and set flags ;ASCII Adjust after Addition ;Decimal (BCD) Adjust after ;alal+bl and set flags ;bxbx+35afh ;ds:(bx)ds:(bx)+al ;clcl+ss:(bp) ;alal+ds:(ebx) ;bxbx+ds:(TEMP+di) ;bxbx+ds:(eax+(2*ecx))
Addition add al, add bx, add [bx], add cl, add al, add bx, add bx,
Increment Examples
inc inc bl BYTE PTR [bx] New MASM Directive: 00ffh inc [bx] 00ffh inc [DATA1] 0100h ;ds:(DATA1)ds:(DATA1)+1 0000h ;Word at ds:(bx)ds:(bx)+1 ;blbl+1 and set flags ;Byte at ds:(bx)ds:(bx)+1 BYTE POINTER
AX CX
0 1
CF=1
CF
BX
AX
add adc
ax, bx,
cx dx
mov mov mov add daa mov mov adc daa mov
1234h 3099h bl dl al bh dh al
;dx1234 BCD ;bx3099 BCD ;al99 BCD ;alcdh illegal BCD, need 34+99=133 ;al33h (33 BCD) and CF=1 ;cl33 BCD ;al30 BCD ;al30h+12h+1=43h ;al43h (43 BCD) not illegal BCD this time ;cx=4333h BCD for 1234+3099
(6ah is incorrect ASCII result j) mov add aaa add ax, al, ax, ;ax0031h=1 ;ax31h+39h=006ah=<nul>j ;ax0100h (this is BCD of result) 3030h ;Convert from BCD to ASCII ;ax0100h+3030h=3130h=10 31h 39h
bx bx
bx
;axax-bx and set flags ;ax(ax-CF)-bx and set flags ;axax-1 ;ax(-1)*(ax) - 2s Complement ;Flag is set according to ax-bx ;Decimal (BCD) Adjust after Subtraction ;ASCII Adjust after Subtraction
Gen Reg
Gen Reg
+ -
Destination
Source
+ Immediate
BX
AX
DI
SI
CF
BX
sub sbb
ax, bx,
di si
Multiplication
8086/8088 One of First to Include mul/div Instruction Allowable Operands: Bytes, Words, DoubleWords Allowable Results: Words, DoubleWords, QuadWords OF, CF Give Useful Information AF, PF, ZF, SF Change but Contents Unpredictable Multiplicand Always in al, ax, eax
Multiply Instructions
Product can be Twice the Size
23=6 2 8 = 16 (same size) (double size, EXT)
OF=CF=0 means product is same size as result (faster) OF=CF=1 means EXT product size (slower) AF, PF, ZF, SF Contents Unpredictable
mul mul mul imul imul imul bl bx ebx bl bx ebx ;axal*bl, Unsigned ;dx:axbx*ax, Unsigned ;edx:eaxebx*eax, Unsigned ;axal*bl, Signed ;dx:axbx*ax, Signed ;edx:eaxebx*eax, Signed
imul imul
Division
8, 16, 32 bit Operands (32 bit is 386+) No Immediate Addressing Mode
idiv
ebx
Logical Instructions
4 3 4 3
Common Usage:
and xor ax, ax ax, ax ;clear CF and OF ;clear ax=CF=OF=PF=AF=SF=0 and ZF=1 ;does more than mov ax, 0h ;faster than push 00h then popf
Masking Operations
(AND) XXXX XXXX (unknown word) 0000 1111 (mask word) 0000 XXXX (result)
test test
al, al,
1 128
;XXXX XXXX (AND) 0000 0001 ;XXXX XXXX (AND) 1000 0000
Shifts
shl Logical Shift Left CF REG 0
shr
REG
CF
sal
sar
CF
Rotates
rol - Rotate Left CF REG
rcl
ror
- Rotate Right
CF REG
rcr
Operand for rotates and shifts can be either: 1) Immediate value 2) Quantity in cl
Classification
a) Jumps - Unconditional control transfers (synchronous) b) Branches - Conditional control transfer c) Interrupts - Unconditional control transfers (asynchronous) d) Iteration - More complex type of branch
LABEL LABEL
UNCONDITIONAL ;next instruction executed has LABEL ;next instruction executed has LABEL ;next instruction executed is after the call ;nothing executed until RESET signal ITERATION ;cx cx - 1, jump to LABEL if cx > 0 ;same as loop but ZF=1 also required ;same as loop but ZF=0 also required INTERRUPTS ;Invoke the int. handler specified by immed8 ;same as int but OF=1 also ;Return from interrupt handler CONDITIONAL to follow
<immed8> <immed8>
jmp
LABEL
3 Forms of jmp
SHORT - 2 bytes, allows jump to 127 locations from current address
EB disp
NEAR
FAR
next:
bx ax ax
Indirect Jump
Address of target is in register
Does NOT add disp to IP - Transfer REG contents to IP
;assume that si contains either 0, 1 or 2 add si, si ;si 2*si add si, OFFSET TABLE ;si si + <address of TABLE> mov ax, cs:[si] ;ax gets an address from the jump table jmp ax ;ip ax ;the following jump TABLE is defined in the code segment!!!! TABLE: DW ZERO DW ONE DW TWO ZERO: ;code for ZERO option . . ONE: ;code for ONE option . . TWO: ;code for TWO option . .
Jump is based on flags used for unsigned number comparison (based on C, Z flag)
ja/jnbe jae/jnb jb/jnae jbe/jna LABEL LABEL LABEL LABEL ;jump ;jump ;jump ;jump if if if if CONDITIONAL CF=ZF=0 - jump above-jump not below/equal CF=0 - jump above/equal-jump not below CF=1 - jump below-jump not above/equal CF=1 or ZF=1 - jump equal - jump zero
Syntax is SETcondition reg8 or mem8 condition includes the suffixes of all conditional jump instructions EXAMPLE
setb seto setz setnc setge T1 T1 al ;T1 1 if CF=1 else T1 0 ;T1 1 if OF=1 else T1 0 ;AL 1 if ZF=1 else AL 0
myFlag ;myFlag 1 if CF=0 else myFlag 0 byte ptr [si] ;set [si] to 1 if SF = OF
AGAIN
100 ;cx 64h - number of words to add OFFSET BLOCK1 ;si offset of BLOCK1 (in ds) OFFSET BLOCK2 ;di offset of BLOCK2 (in es) ;Auto-increment si and di, DF=0 di ;bx di, save offset of BLOCK2 ;ax ds:[si], sisi+2, didi+2 [bx] ;ax ax + ds:[bx] bx ;di bx, restore di with ; offset in BLOCK2 ;es:[di] ax, sisi+2, didi+2 ;cx cx - 1, if cx0 jump to AGAIN ;ip ss:[sp]
ADDS
Procedures
Group of instructions that perform single task
(can be used as) a SUBROUTINE
call ret
- invokes subroutine - pushes ip - returns from subroutine - pops ip PROC and ENDP
Must specify
NEAR FAR - intrasegment - intersegment
call Instruction
Differs from jmp since return address on stack NEAR call: FAR call: 3 bytes - 1 opcode and 2 for IP 5 bytes - 1 opcode, 2 for IP and 2 for CS
call with operand - can use 16-bit offset in any register except segment registers
call bx ;pushes ip then jumps to cs:[bx]
COMP
COMP
mov call
COMP
COMP
al
;Save current contents of dx ;dx 03f8h (an immediate data Xfer) ;al receives 1 byte of data from I/O ; device with output port address 03f8h ;dx03f9h ;send 1 byte of data to I/O device ; input port with address 03f9h ;restore dx to value at call time ;ipss:[sp], spsp+2
COMP
call Instruction with Indirect Address Useful for choosing different subroutines at runtime Can use a table (like the jump table example)
;Assume bx contains 1, 2 or 3 for subroutine desired TABLE DW ONE DW TWO DW THREE dec bx add bx, bx mov di, OFFSET TABLE call cs:[bx+di] jmp CONT ONE PROC NEAR ONE ENDP TWO PROC NEAR TWO ENDP THREE PROC NEAR THREE ENDP CONT: nop
ret Instruction
NEAR FAR pops 16-bit value places in IP pops 32-bit value places in CS:IP
Type is determined by PROC directive Other form of ret has immediate operand (8 bit)
The immediate operand is added to the SP after popping the return address
Example ret 6
;cl get al ;if MSB of al=0 then ;ch gets 00h ;else ch gets ffh ;move string with zero extended
movzx
SEGMENT BYTE 'DATA' ;Define the data segment DB 'This is string 1',13,10,'$' ;Define the first string DB 'THIS IS STRING 2',13,10,'$' ;Define the second string DB 13,10,'$' ;Space to new line string ENDS SEGMENT mov ax, mov ds, mov es, BYTE 'CODE' ;Define SEG strngdat ax ax the ;ax ;ds ;es Code segment <-- data segment start address <-- initialize data segment register <-- initialize extra segment register
Print the strings to the display before moving them mov lea int lea int lea int ah, dx, 21h dx, 21h dx, 21h 9 strng1 strng2 crlf ;ah <-- 9 DOS 21h string function ;dx <-- offset of first string ;DOS service interrupt ;dx <-- offset of second string ;DOS service interrupt ;dx <-- offset of crlf string ;DOS service interrupt
Print the strings to the display after moving them mov lea int lea int lea int ; ; ; ah, dx, 21h dx, 21h dx, 21h 9 strng1 strng2 crlf ;ah <-- 9 DOS 21h string function ;dx <-- offset of first string ;DOS service interrupt ;dx <-- offset of second string ;DOS service interrupt ;dx <-- offset of crlf string ;DOS service interrupt
Invoke DOS interrupt that returns processor to OS mov ax, 4c00h ;ax <-- 4c DOS 21h program halt ;DOS service interrupt ENDS
Assembling/Linking
lodsw
lodsd
stosb
stosw
stosd
bsf
bsr
;di address of memory location BLOCK ;DF 0, auto-increment mode ;cx 64h, initialize counter to 100 ;clear al ;test for 00h in location es:di ;if es:di not equal to 00h then ; cx cx - 1, di di + 1, repeat ;else if cx = 00h ; do not repeat test ;else if es:di equals 00h ; ZF = 1, do not repeat test
STRING ;di offset of memory location labeled STRING ;DF=0 auto-increment mode 256 ;cx ffh, initialize counter to 256 20h ;al , an ASCII <space> Character ;while es:di=20h, continue scanning ;when cx=0 or es:di not equal 20h stop ;after stopping cx contains offset from ;STRING where first non-20h resides (if not 0)
STRING ;di offset of memory location labeled STRING ;DF=0 auto-increment mode 256 ;cx ffh, initialize counter to 256 20h ;al , an ASCII <space> Character ;while es:di=20h, continue scanning ;when cx=0 or es:di not equal 20h stop ;after stopping cx contains offset from ;STRING where first non-20h resides (if not 0)