1 / 51

Chapter 4: Using the Assembler

Chapter 4: Using the Assembler. Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition. Overview. More about the assembler and linker Operators and expressions JMP and LOOP instructions Indirect addressing Debugging workshop More 80386 and 80486 instructions

nola-dennis
Download Presentation

Chapter 4: Using the Assembler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Using the Assembler Slides to Accompany Assembly Language for Intel-Based Computers, Third Edition Irvine, Kip R. Assembly Language For Intel-Based Computers

  2. Overview • More about the assembler and linker • Operators and expressions • JMP and LOOP instructions • Indirect addressing • Debugging workshop • More 80386 and 80486 instructions • Using a link library Irvine, Kip R. Assembly Language For Intel-Based Computers

  3. More About the Assembler and Linker Topics: • Source Listing • Map File • Memory models • Overlapping segments • Target processor directives Irvine, Kip R. Assembly Language For Intel-Based Computers

  4. Source Listing • Exchange Two Variables                       (SAMPLE.ASM) • 3 0000 .model small • 4 0000 .stack 100h • 5 • 6 0000 .code • 7 0000 main proc • 8 0000  B8 0000s mov   ax,@data ; initialize DS register • 9 0003  8E D8 mov   ds,ax • 10 0005 swap: • 11 0005  A0 0000r mov   al,value1 ; load the AL register • 12 0008  86 06 0001r xchg  al,value2 ; exchange AL, value2 • 13 000C  A2 0000r mov   value1,al ; store new value of AL • 14 000F  B8 4C00 mov   ax,4C00h ; return to DOS • 15 0012  CD 21 int   21h • 16 0014 main endp • 17 • 18 0014 .data • 19 0000  0A value1  db 0Ah • 20 0001  14 value2  db 14h • 21 • 22 end main Irvine, Kip R. Assembly Language For Intel-Based Computers

  5. Map File Segments always begin on even-paragraph boundaries. • Start Stop Length Name Class • 00000H   00013H  00014H  _TEXT CODE • 00020H   00021H  00002H    _DATA      DATA • 00030H   0012FH  00100H    STACK      STACK • Program entry point at 0000:0000 Irvine, Kip R. Assembly Language For Intel-Based Computers

  6. Memory Models Irvine, Kip R. Assembly Language For Intel-Based Computers

  7. Overlapping Segments Start Stop Length Name Class 00000 00010 00011 _TEXT CODE 00020 0002F 00010 _DATA DATA 00030 0012F 00100 STACK STACK Program entry point at 0000:0000 Irvine, Kip R. Assembly Language For Intel-Based Computers

  8. Target Processor Directives Irvine, Kip R. Assembly Language For Intel-Based Computers

  9. Assembler Operators Irvine, Kip R. Assembly Language For Intel-Based Computers

  10. Assembler Operators(continued) Irvine, Kip R. Assembly Language For Intel-Based Computers

  11. Operator Precedence Table Irvine, Kip R. Assembly Language For Intel-Based Computers

  12. Offset, Seg, Ptr, Label, and Even • OFFSET returns the 16-bit address (offset) of a label • SEG returns the segment portion of a label's address • PTR overrides the default size of an operand • LABEL redefines the size attribute of a data label • EVEN aligns the next instruction to a 16-bit boundary • EVENDATA aligns the next data label on a 16-bit boundary Irvine, Kip R. Assembly Language For Intel-Based Computers

  13. TYPE and SIZE Operators • TYPE • returns the size, in bytes of a single element of a data label (variable) • LENGTH • returns a count of the number of individual elements in a data label that uses the DUP operator • SIZE • returns the product of TYPE * LENGTH Irvine, Kip R. Assembly Language For Intel-Based Computers

  14. TYPE TYPE returns the size attribute: .data myByte db 1,2,3,4 myWord dw 1000h,2000h,3000h myDouble dd 12345678h myQuad dq 1,2,3 .code mov ax,TYPE myByte ; 1 mov ax,TYPE myWord ; 2 mov ax,TYPE myDouble ; 4 mov ax,TYPE myQuad ; 8 Irvine, Kip R. Assembly Language For Intel-Based Computers

  15. LENGTH Returns a count of the number of individual elements in a data label that uses the DUP operator: .data myByte db 20 dup(?) myWord dw 5 dup(0) .code mov ax,LENGTH myByte ; 20 mov ax,LENGTH myWord ; 5 Irvine, Kip R. Assembly Language For Intel-Based Computers

  16. SIZE Returns TYPE multiplied by LENGTH: .data myByte db 20 dup(?) myWord dw 5 dup(0) .code mov ax,SIZE myByte ; 20 (20 * 1) mov ax,SIZE myWord ; 10 (5 * 2) Irvine, Kip R. Assembly Language For Intel-Based Computers

  17. JMP and LOOP Instructions • JMP is an unconditional jump to a code label • LOOP creates a counting loop, using CX as the default counter • LOOPD uses ECX as the counter register • LOOPW uses CX as the counter register • (only necessary in 32-bit mode programming) Irvine, Kip R. Assembly Language For Intel-Based Computers

  18. JMP: Distance Modifiers • JMP SHORT destination • +/- 127 bytes • JMP NEAR PTR destination • same code segment • (default in the small and compact memory models) • JMP FAR PTR destination • different code segment • (default in the large, medium, and huge memory models) Irvine, Kip R. Assembly Language For Intel-Based Computers

  19. JMP Example Label1: . . jmp Label1 Unconditional Transfer of control to a label: Irvine, Kip R. Assembly Language For Intel-Based Computers

  20. LOOP Instruction • Automatically uses CX as the counter • decrements it automatically • If CX > 0, LOOP transfers control to a label • otherwise, excecution drops through Irvine, Kip R. Assembly Language For Intel-Based Computers

  21. LOOP Example Task: sum the integers { 1,2,3,4,5 } mov cx,5 ; loop counter mov bx,1 ; value to be added mov ax,0 ; holds the sum L1: add ax,bx inc bx Loop L1 ; AX=000F, BX=0006, CX=0000 Irvine, Kip R. Assembly Language For Intel-Based Computers

  22. Indirect Addressing • Indirect Operands [si]. [di], [bx], [bp] • Based and Indexed Operands array[si], array[di], array[bx] • Base-Index Operands [bx+si], [bx+di] • Base-Index with Displacement array[bx+si], array[bx+di] Irvine, Kip R. Assembly Language For Intel-Based Computers

  23. Indirect Operand Example .data aString db "ABCDEFG“ .code mov bx,offset aString add bx,5 mov dl,[bx] Irvine, Kip R. Assembly Language For Intel-Based Computers

  24. Adding 8-bit Integers .data aList db 10h,20h,30h sum   db 0 .code mov bx,offset aList    mov al,[bx] ; AL = 10h inc bx add al,[bx] ; AL = 30h inc bx add al,[bx] ; AL = 60h mov si,offset sum ; get offset of sum mov [si],al ; store the sum If you want to paste a code example such as this into a program, remember that the code segment must always begin with the following statements: mov ax,@data mov ds,ax Irvine, Kip R. Assembly Language For Intel-Based Computers

  25. Adding 16-bit Integers .data wordList dw 1000h,2000h,3000h, 0 .code mov bx,offset wordList mov ax,[bx] ; first number add ax,[bx+2] ; second number add ax,[bx+4] ; third number mov [bx+6],ax ; store the sum Irvine, Kip R. Assembly Language For Intel-Based Computers

  26. 32-Bit Registers The .386 directive permits any of the following registers to be used as indirect operands: EAX, EBX, ECX, EDX, ESI, EDI, EBP .386 mov ax,[ebx+3] mov dl,string[edx] mov ecx,[esi] mov ebx,[eax] Irvine, Kip R. Assembly Language For Intel-Based Computers

  27. Displaying a String .data string db "This is a string." COUNT = ($–string)  ; calculate string length .code mov   cx,COUNT ; loop counter mov   si,offset string  L1:  mov   ah,2 ; DOS function: display char mov   dl,[si] ; get character from array int   21h ; display it now inc   si ; point to next character Loop  L1 ; decrement CX, repeat until 0 Irvine, Kip R. Assembly Language For Intel-Based Computers

  28. Summing an Integer Array .data intarray dw 0100h,0200h,0300h,0400h COUNT = ($ – intarray) / (TYPE intarray) .code mov   ax,0 ; zero accumulator mov   di,offset intarray ; address of array mov   cx,COUNT ; loop counter L1:  add   ax,[di] ; add an integer add   di,TYPE intarray ; point to next integer Loop  L1 ; repeat until CX = 0 Irvine, Kip R. Assembly Language For Intel-Based Computers

  29. Based and Indexed Operands The microsoft assembler permits the same address expression to be notated in various ways: Irvine, Kip R. Assembly Language For Intel-Based Computers

  30. Two-Dimensional Array Example Each row of this table contains five bytes. BX points to the beginning of the second row: .data ROWSIZE = 5 array  db  2h, 16h, 4h, 22h, 13h db 19h, 42h, 64h, 44h, 88h .code mov bx,ROWSIZE mov al,array[bx]     ; AL = 19h Irvine, Kip R. Assembly Language For Intel-Based Computers

  31. Based-Index Operands Add the value of a base register to an index register, producing an effective address of 0157: BX = 0155, SI = 0002 Example... Irvine, Kip R. Assembly Language For Intel-Based Computers

  32. Base-Index Example .data ROWSIZE = 5 array  db  10h, 20h, 30h, 40h, 50h db 60h, 70h, 80h, 90h,0A0h        db 0B0h,0C0h,0D0h,0E0h,0F0h .code mov  bx,offset array ; point to the array at 0150 add  bx,ROWSIZE ; choose second row mov  si,2 ; choose third column mov  al,[bx + si] ; get the value at 0157 Irvine, Kip R. Assembly Language For Intel-Based Computers

  33. Base-Index with Displacement .data ROWSIZE = 5 array db  10h, 20h, 30h, 40h, 50h db 60h, 70h, 80h, 90h,0A0h       db 0B0h,0C0h,0D0h,0E0h,0F0h .code mov bx,ROWSIZE ; row 1 mov si,2 ; column 2 mov dl,array[bx + si] ; DL = 80h Irvine, Kip R. Assembly Language For Intel-Based Computers

  34. Debugging Workshop Irvine, Kip R. Assembly Language For Intel-Based Computers

  35. Mismatching Operand Sizes 1:    title Mismatching Operand Sizes  2:     3:    .model small 4:    .stack 100h 5:    .code 6:    main proc 7: mov   ax,@data 8: mov   ds,ax 9: mov   ax,value1     10: mov   ah,value2 11: mov   ax,4C00h 12: int   21h 13: main endp 14: 15:   .data 16:  value1   db    0Ah 17:  value2   dw    1000h 18:  end  main  (9): warning A4031: Operand types must match (10): warning A4031: Operand types must match Irvine, Kip R. Assembly Language For Intel-Based Computers

  36. Miscellaneous Errors  1:   title Miscellaneous Errors Program  2:     3:   .model small  4:   .stack 100h  5:   .code  6:   main proc  7:        mov   ax,@data  8:        mov   ds,ax  9:        mov   ax,bx * cx 10:        mov   bx,value1 * 2 11:        mov   byte ptr value3,al 12:        mov   cx,ax 13:        mov   cs,ds 14:        mov   ax,4C00h 15:        int   21h 16:    main endp 17:   .data 18:   value1   db    0Ah 19:   value2   db    14h 20:   value3   dw    1000h 21:   end main Irvine, Kip R. Assembly Language For Intel-Based Computers

  37. Intel386 and Intel486 Instructions • MOVZX - Move with zero-extend • moves 8-bit operand into a 16-bit register, fills upper bits with zeros • moves 16-bit operand into a 32-bit register, fills upper bits with zeros • MOVSX - Move with sign-extend • moves 8-bit operand into a 16-bit register, sign extends into upper bits • moves 16-bit operand into a 32-bit register, sign extends into upper bits Irvine, Kip R. Assembly Language For Intel-Based Computers

  38. MOVZX and MOVSX Examples .data var16 dw 1234h var8 db -2 ; FEh .code mov bl,22h movzx ax,bl ; AX = 0022h movzx edx,var16 ; EDX = 00001234h movsx cx,var8 ; CX = FFFEh Irvine, Kip R. Assembly Language For Intel-Based Computers

  39. XADD Instruction .code mov   ax,1000h  mov   bx,2000h ; AX = 1000h, BX = 2000h xadd  ax,bx ; AX = 3000h, BX = 1000h XADD op-left, op-right Adds operands and stores sum in op-left. Saves the starting value of op-left and stores it in op-right. Irvine, Kip R. Assembly Language For Intel-Based Computers

  40. Using a Link Library • IRVINE.LIB is supplied with the book's sample programs • Complete list of procedures is in Appendix E • EXTRN directive • notifies the assembler that a procedure, constant, or variable is located outside the current program module • EXTRN name:type Irvine, Kip R. Assembly Language For Intel-Based Computers

  41. Data Types for the EXTRN Directive Irvine, Kip R. Assembly Language For Intel-Based Computers

  42. Selected Procedures in the Link Library Irvine, Kip R. Assembly Language For Intel-Based Computers

  43. Selected Procedures in the Link Library Irvine, Kip R. Assembly Language For Intel-Based Computers

  44. Link Library Demo Program Title Link Library Demo Program        (lnkdemo.asm) ; This program calls various I/O procedures ; in the link library. .model small .stack 100h WhiteOnBlue = 1Fh GreetingLoc = 0400h .data greeting db "Link Library Demo Program" db 0dh,0ah,0dh,0ah db "What is your name? ",0   numberPrompt db 0dh,0ah db "Please enter a 16–bit integer: ",0 userName db 50 dup(0) pressAnyKey  db 0dh,0ah,0dh,0ah db "Press any key...",0 .code extrn Clrscr:proc, Crlf:proc, Gotoxy:proc, \   Readint:proc, Readstring:proc, Scroll:proc, \   Readkey:proc, Writeint:proc, Writestring:proc Irvine, Kip R. Assembly Language For Intel-Based Computers

  45. Link Library Demo, continued main proc    mov ax,@data    mov ds,ax ; Clear the screen, scroll a blue window.    call Clrscr    mov  cx,0400h ; upper–left corner    mov  dx,0B28h ; lower–right corner    mov  bh,WhiteOnBlue    call Scroll ; Display a greeting and ask for the  ; user’s name.    mov  dx,GreetingLoc    call Gotoxy    mov  dx,offset greeting    call Writestring    mov  dx,offset userName    call Readstring Irvine, Kip R. Assembly Language For Intel-Based Computers

  46. Link Library Demo, continued ; Ask the user to enter a signed decimal integer. ; Redisplay the number in hexadecimal and binary.    mov  dx,offset numberPrompt    call Writestring    call Readint ; input an integer    call Crlf    mov  bx,16 ; display in hexadecimal    call Writeint    call Crlf    mov  bx,2 ; display in binary    call Writeint       mov  dx,offset pressAnyKey    call Writestring    call Readkey    call Clrscr    mov ax,4c00h ; end program    int 21h main endp    end main Irvine, Kip R. Assembly Language For Intel-Based Computers

  47. Generating Random Integers Generate 20 random integers between 0 and 999. .code     extrn Randomize:proc, Random_range:proc extrn WriteInt:proc, Crlf:proc     call Randomize     mov  cx,20 L1: mov  eax,1000     call Random_range  ; EAX = random integer     mov  bx,10         ; decimal radix     call WriteInt         call Crlf     Loop L1 Irvine, Kip R. Assembly Language For Intel-Based Computers

  48. Delay_seconds Procedure Pause for a specified number of seconds. extrn Seconds_today:proc Delay_seconds proc    pusha    mov  ecx,eax          ; delay, in seconds    call Seconds_today    mov  ebx,eax          ; save start time DLY1:     call Seconds_today    ; get the time    sub  eax,ebx          ; subtract from start    cmp  eax,ecx          ; delay finished yet?    jb   DLY1             ; if not, continue loop    popa    ret Delay_seconds endp Irvine, Kip R. Assembly Language For Intel-Based Computers

  49. The End Irvine, Kip R. Assembly Language For Intel-Based Computers

  50. title text .code Irvine, Kip R. Assembly Language For Intel-Based Computers

More Related