Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Assembly Language Chapter 4 S. Dandamudi.

Similar presentations


Presentation on theme: "Overview of Assembly Language Chapter 4 S. Dandamudi."— Presentation transcript:

1 Overview of Assembly Language Chapter 4 S. Dandamudi

2 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 2 Outline Assembly language statements Data allocation Where are the operands?  Addressing modes »Register »Immediate »Direct »Indirect Data transfer instructions  mov, xchg, and xlat  Ambiguous moves Overview of assembly language instructions  Arithmetic  Conditional  Iteration  Logical  Shift/Rotate Defining constants  EQU, %assign, %define Macros Illustrative examples Performance: When to use the xlat instruction

3 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 3 Overview of Assembly Instructions (cont’d) Logical Instructions  Format: and destination,source or destination,source xor destination,source not destination  Semantics: »Performs the standard bitwise logical operations –result goes to destination  TEST is a non-destructive AND instruction test destination,source  Performs logical AND but the result is not stored in destination (like the CMP instruction)

4 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 4 Overview of Assembly Instructions (cont’d) Logical Instructions Example: Testing the value in AL for odd/even number test AL,01H ; test the least significant bit jz even_number odd_number: jmp skip1 even_number: skip1:...

5 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 5 Assembly Program - Logical Instructions ;Assembly program that reads a number then prints ;if it is even or odd number.586 option segment:use16 dosseg.model small.data msg db 'Enter a number between 0 and 9:$' eventxt db 0dh,0ah,'The input Number is EVEN$' oddtxt db 0dh,0ah,'The input Number is ODD$'

6 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 6 Assembly Program - Logical Instructions.code start:.startup mov dx,offset msg mov ah,9h int 21h mov ah,1h ;input a number character int 21h sub al,30h ;convert to hex number test al,01h ; test the number using and jz evenlab ;jump if anding == 0 mov dx,offset oddtxt mov ah,9h ;print odd message int 21h

7 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 7 Assembly Program - Logical Instructions jmp exit evenlab:mov dx,offset eventxt ;even section mov ah,9h ;print even message int 21h exit:mov ax,4c00h ;exit program int 21h end

8 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 8 Overview of Assembly Instructions (cont’d) Shift Instructions  Format: Shift left shl destination,count shl destination,CL Shift right shr destination,count shr destination,CL  Semantics: »Performs left/right shift of destination by the value in count or CL register – CL register contents are not altered

9 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 9 Overview of Assembly Instructions (cont’d) Shift Instructions Bit shifted out goes into the carry flag »Zero bit is shifted in at the other end

10 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 10 Overview of Assembly Instructions (cont’d) Shift Instructions  count is an immediate value shl AX,5  Specification of count greater than 31 is not allowed »If a greater value is specified, only the least significant 5 bits are used  CL version is useful if shift count is known at run time » E.g. when the shift count value is passed as a parameter in a procedure call »Only the CL register can be used  Shift count value should be loaded into CL mov CL,5 shl AX,CL

11 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 11 Overview of Assembly Instructions (cont’d) Shift Instructions

12 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 12 Assembly Program -Shift Instructions ;Assembly program that reads a number and ;then prints the input number in binary.586 option segment:use16 dosseg.model small.data msg db 'Enter a number between 0 and 9:$' resout db 0dh,0ah,'The input Number in binary:',0dh,0ah,'$'

13 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 13 Assembly Program -Shift Instructions.code start:.startup mov dx,offset msg mov ah,9h int 21h mov ah,1h ;read a number character int 21h mov bl,al sub bl,30h mov dx,offset resout mov ah,9h int 21h

14 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 14 Assembly Program -Shift Instructions mov cx,8 loops:shl bl,1 ;shift bl by 1 position to left jc print_1 ;jump to print 1 if carry=1 mov dl,'0' ;print 0 if else mov ah,2h int 21h loop loops jmp exit

15 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 15 Assembly Program -Shift Instructions print_1:mov dl,'1' mov ah,2 int 21h loop loops exit:mov ax,4c00h int 21h end

16 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 16 Overview of Assembly Instructions (cont’d) Rotate Instructions  Two types of ROTATE instructions  Rotate without carry »rol (ROtate Left) »ror (ROtate Right)  Rotate with carry »rcl (Rotate through Carry Left) »rcr (Rotate through Carry Right)  Format of ROTATE instructions is similar to the SHIFT instructions »Supports two versions –Immediate count value –Count value in CL register

17 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 17 Overview of Assembly Instructions (cont’d) Rotate Instructions  Rotate without carry

18 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 18 Overview of Assembly Instructions (cont’d) Rotate Instructions  Rotate without carry

19 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 19 Overview of Assembly Instructions (cont’d) Rotate Instructions  Rotate with carry

20 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 20 Overview of Assembly Instructions (cont’d) Rotate Instructions  Rotate with carry

21 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 21 Assembly Program-Rotate Instructions ;Assembly program that reads a number and ;then prints the input number in binary with 4 bits rotation.586 option segment:use16 dosseg.model small.data msg db 'Enter a number between 0 and 9:$' resout db 0dh,0ah,'The input Number in binary:',0dh,0ah,'$'

22 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 22 Assembly Program-Rotate Instructions.code start:.startup mov dx,offset msg mov ah,9h int 21h mov ah,1h ;read a number character int 21h mov bl,al sub bl,30h rol bl,4 ;rotate 4-bit contents in bl

23 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 23 Assembly Program-Rotate Instructions mov dx,offset resout mov ah,9h int 21h mov cx,8 loops:shl bl,1 ;shift bl by 1 position to left jc print_1 ;jump to print 1 if carry=1 mov dl,'0' ;print 0 if else mov ah,2h int 21h loop loops jmp exit

24 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 24 Assembly Program-Rotate Instructions print_1:mov dl,'1' mov ah,2 int 21h loop loops exit:mov ax,4c00h int 21h end

25 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 25 Defining Constants MASM provides directive:  EQU directive »No reassignment »Only numeric constants are allowed

26 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 26 Defining Constants Defining constants has two main advantages  Improves program readability NUM_OF_STUDENTS EQU 90.... mov CX,NUM_OF_STUDENTS is more readable than mov CX,90  Helps in software maintenance »Multiple occurrences can be changed from a single place Convention  We use all upper-case letters for names of constants

27 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 27 Defining Constants The EQU directive Syntax: name EQU expression  Assigns the result of expression to name  The expression is evaluated at assembly time Examples NUM_OF_ROWS EQU 50 NUM_OF_COLS EQU 10 ARRAY_SIZE EQU NUM_OF_ROWS * NUM_OF_COLS

28 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 28 Defining Constants: Example ;Assembly program to print values of ;constants x, y, and z.586 option segment:use16.model small.data msg1 db 'x=$' msg2 db 0ah,0dh,'y=$' msg3 db 0ah,0dh,'z=$'

29 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 29 Defining Constants: Example X EQU 2 Y EQU X+2 Z EQU Y*2.code.startup mov dx,offset msg1 mov ah,9 int 21h mov dl,X add dl,'0'

30 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 30 Defining Constants: Example mov ah,2 int 21h mov dx,offset msg2 mov ah,9 int 21h mov dl,Y add dl,30h mov ah,2 int 21h.

31 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 31 Defining Constants: Example mov dx,offset msg3 mov ah,9 int 21h mov dl,Z add dl,30h mov ah,2 int 21h.exit end.

32 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 32 Macros Macros are just like procedures, but not really. Macros look like procedures, but they exist only until your code is compiled. After compilation all macros are replaced with real instructions. If you declared a macro and never used it in your code, compiler will simply ignore it.

33 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 33 Macros (cont’d) Macro definition: name MACRO [parameters,...] ENDM Macros should be defined above the code that uses it.

34 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 34 Macros (cont’d) Example (Macro with 3 parameters) MyMacro MACRO p1, p2, p3 MOV AX, p1 MOV BX, p2 MOV CX, p3 ENDM.CODE … MyMacro 1, 2, 3 MyMacro 4, 5, DX … END

35 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 35 Macros (cont’d) The previous code is expanded into: MOV AX, 00001h MOV BX, 00002h MOV CX, 00003h MOV AX, 00004h MOV BX, 00005h MOV CX, DX MyMacro 1, 2, 3 MyMacro 4, 5, DX

36 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 36 Macros : Example 1 ;Assembly program to add 5 numbers and ;print the result on screen using macro.586 DOSSEG OPTION SEGMENT: USE16.MODEL SMALL.DATA numbers db 1,2,3,1,2

37 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 37 Macros : Example 1 print_info MACRO add dl,'0' mov ah,2h ;print the sum int 21h ENDM.CODE START:.startup mov bx,offset numbers mov cx,5 ;initialize counter mov dl,0

38 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 38 Macros : Example 1 target:add dl,[bx] inc bx dec cx jnz target ; jump if cx<>0 print_info ;call macro mov AX, 4C00h int 21h END

39 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 39 Macros : Example 2 ;Assembly program to add 3 numbers and ;print the result on screen using macros print_info MACRO add dl,'0' mov ah,2h ;print the sum int 21h ENDM

40 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 40 Macros : Example 2 add_num macro x,y,z mov dl,x add dl,y add dl,z Endm.586 DOSSEG OPTION SEGMENT: USE16.MODEL SMALL

41 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 41 Macros : Example 2.DATA numbers db 1,2,3.CODE START:.startup add_num numbers,numbers+1,numbers+2 print_info mov AX, 4C00h int 21h END

42 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 42 Macros : Example 3 ;Assembly program to continuous reading a ;character and print the next until read ‘*’ ;(using read and print macros). read_ch Macro mov ah,1 ;read a character int 21h endm

43 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 43 Macros : Example 3 print_ch macro y mov dl,y mov ah,2 ;print next char int 21h mov dl,0dh ;print new line mov ah,2 int 21h mov dl,0ah int 21h endm

44 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 44 Macros : Example 3.586 DOSSEG OPTION SEGMENT: USE16.MODEL SMALL.CODE START:.startup target: read_ch cmp al,'*' je exit ;jump to exit if al==‘*’ inc al ;find next character

45 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 45 Macros : Example 3 print_ch al jmp target ;continuous loop exit:mov AX, 4C00h int 21h END

46 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 46 Macros : Example 4 ;Assembly program to continuous reading a ;character and print the next until ;read ‘*’ (using macros). read_ch Macro mov ah,1 ;read a character int 21h endm

47 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 47 Macros : Example 4 print_ch macro y mov dl,y mov ah,2 ;print next char int 21h mov dl,0dh ;print new line mov ah,2 int 21h mov dl,0ah int 21h endm

48 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 48 Macros : Example 4 exit_p macro mov ax,4c00h int 21h endm

49 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 49 Macros : Example 4 do_loop macro target:.startup read_ch cmp al,'*' je exit ;jump to exit if al==‘*’ inc al ;find next character print_ch al jmp target ;continuous loop exit:exit_p endm

50 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 50 Macros : Example 4.586 DOSSEG OPTION SEGMENT: USE16.MODEL SMALL.CODE do_loop END

51 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 51 Case study ;Assembly program to add 2 numbers [0-9] and ;print the sum on screen using macros print_msg macro message mov dx,offset message mov ah,9 int 21h endm

52 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 52 Case study print_char macro char mov dl,char add dl,30h mov ah,2 int 21h Endm read_char macro mov ah,1 int 21h endm

53 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 53 Case study newln macro mov dl,0dh mov ah,2 int 21h mov dl,0ah int 21h endm

54 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 54 Case study print_sum macro bh,bl and bl,0fh print_msg res shr bh,4h print_char bh print_char bl endm

55 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 55 Case study.586 option segment:use16.model small.data msg db 'Enter a number [0-9]:$' res db 'The sum =$'.code.startup print_msg msg read_char

56 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 56 Case study mov bl,al sub bl,30h newln print_msg msg read_char sub al,30h add bl,al newln add bl,6

57 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  S. Dandamudi Chapter 4: Page 57 Case study mov bh,bl print_sum bh,bl mov ah,4ch int 21h end


Download ppt "Overview of Assembly Language Chapter 4 S. Dandamudi."

Similar presentations


Ads by Google