270 likes | 577 Views
Assembly Instructions. Assembly language instructions may involve mnemonics, labels, variables, constants, and directives. Examples are as follows. here 1 mov 5 ax,23 ;set ax value msg 2 db 6 "help$" ;ASCII bytes assigned to msg
E N D
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
Define Byte, Word Define Byte Variables (memory locations) Define Word Variables (memory locations)
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
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
Pointer Given: message db ‘ABC’ ;define 3 bytes P db message ;P points to message The variable P contains the offset of message
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
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
Based Index Addressing ExampleAdding 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
Based Index Addressing ExampleAdding 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
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
Assemble-Link-Execute Cycle .asm MASM .exe .obj .lst .map
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
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
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
XCHGing Two Variables Cannot do: xchg mem1,mem2, but
Arithmetic InstructionsINC and DEC Instructions inc destination ;add 1 to destination dec destination ;subtract 1 from destination where destinationisregormem 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
Arithmetic InstructionsADD Instruction add destination, source Example: add ax,bx Contents of Registers BeforeAfter add ax,bx AX |0FFF |1000 | BX |0001 |0001 | add ax,bx AX |0002 |0001 | plus a BX |FFFF |FFFF | carry
Arithmetic InstructionsADD 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
Arithmetic InstructionsSUB Instruction sub destination, source Example: sub ax,bx Contents of Registers BeforeAfter sub ax,bx AX |00A0 |009F | BX |0001 |0001 | sub ax,bx AX |0005 |FFFF | BX |0006 |0006 |
Arithmetic InstructionsMUL Instruction mul multiplier ;multiplicand in ax ;product in dx,ax Example: mul bx Contents of Registers BeforeAfter mul bx AX |FFFF |FFF0 | BX |0010 |0010 | DX |0000 |000F |
Arithmetic InstructionsDIV Instruction div divisor ;dividend in dx,ax: quotient in ax ;remainder in dx Example: div bx Contents of Registers BeforeAfter div bx AX |FFF1 |FFFF | BX |0010 |0010 | DX |000F |0001 |
Memory Models Produces .com files What we will use Linear addressing