Kaizen cso002 l1

770 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
770
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Kaizen cso002 l1

  1. 1. CX323L1 COMPUTER SYSTEM ORGANIZATION
  2. 2. COMPUTER SYSTEM ORGANIZATION Prelim Midterm Finals Kaizen 2006 - 2007 TERMS
  3. 3. PRELIM Week 1 – Microprocessor Week 2 – Basic Structure of hardware and Softw Week 3 – Register Week 4 – System Operation Week 5 - Editor
  4. 4. Microprocessors Topics: 1.Microprocessor 2.History of Microprocessors 3.Types of Microprocessors 4.Number System * Decimal * Binary * Octal * Hexadecimal
  5. 5. Objectives •To define what is a microprocessor. • To discuss the history of microprocessor. • Identify the types of microprocessor • To recall numbers systems.
  6. 6. Microprocessors The brain or engine of the PC is the processor (sometimes called microprocessor), or central processing unit (CPU). The CPU performs the system's calculating and processing.
  7. 7. Brief History • Intel is generally credited with creating the first microprocessor in 1971 with the introduction of a chip called the 4004. • All PC-compatible systems use either Intel processors or Intel- compatible processors from a handful of competitors Example: AMD or Cyrix. •Late 1970s the two most popular processors for PCs were not from Intel (although one was a clone of an Intel processor). Personal computers of that time primarily used the Z-80 by Zilog and the 6502 by MOS Technologies. • Z-80 was noted for being an improved and less expensive clone of the Intel 8080 processor, similar to the way companies today such as AMD, Cyrix, IDT, and Rise Technologies have cloned Intel's Pentium processors.
  8. 8. Types of Microprocessor Processors can broadly be divided into the categories of: CISC, RISC, hybrid, and special purpose. Complex Instruction Set Computers (CISC) have a large instruction set, with hardware support for a wide variety of operations. In scientific, engineering, and mathematical operations with hand coded assembly language (and some business applications with hand coded assembly language), CISC processors usually perform the most work in the shortest time.
  9. 9. Reduced Instruction Set Computers (RISC) have a small, compact instruction set. In most business applications and in programs created by compilers from high level language source, RISC processors usually perform the most work in the shortest time. Hybrid processors are some combination of CISC and RISC approaches, attempting to balance the advantages of each approach. Special purpose processors are optimized to perform specific functions. Digital signal processors and various kinds of co- processors are the most common kinds of special purpose processors.
  10. 10. Function of Microprocessor • To coordinate the functioning of the various components of the system by generating and responding to control signals and to perform various arithmetic and logic functions.
  11. 11. Number Systems Decimal, Binary, Octal and Hexadecimal Numeric Systems. Conversions among the different systems
  12. 12. Decimal Numbering systems Base: 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  13. 13. Example: 523410 103 = 1000 102 = 100 101 = 10 100 = 1 5 2 3 4 5,234 = 5 x 1000 + 2 x 100 + 3 x 10 + 4 x 1
  14. 14. Binary Numbering systems • Base: 2 • Digits: 0, 1 • binary number: 1101012 positional powers of 2: 25 24 23 22 21 20 decimal positional value: 32 16 8 4 2 1 binary number: 1 1 0 1 0 1
  15. 15. Binary to Decimal Conversion • To convert to base 10, add all the values where a one digit occurs. Ex: 1101012 positional powers of 2: 25 24 23 22 21 20 decimal positional value: 32 16 8 4 2 1 binary number: 1 1 0 1 0 1 32 + 16 + 4 + 1 = 5310
  16. 16. Ex: 1010112 positional powers of 2: 25 24 23 22 21 20 decimal positional value: binary number:
  17. 17. Ex: 1010112 positional powers of 2: 25 24 23 22 21 20 decimal positional value: 32 16 8 4 2 1 binary number: 1 0 1 0 1 1 32 + 8 + 2 + 1 = 4310
  18. 18. The Division Method. Divide by 2 until you reach zero, and then collect the remainders in reverse. Ex 1: 5610 = 1110002 2 ) 56 Rem: 2 ) 28 0 2 ) 14 0 2 ) 7 0 2 ) 3 1 2 ) 1 1 0 1
  19. 19. Ex 2: 3510 = 2 ) Rem: 2 ) 2 ) 2 ) 2 ) 2 ) Answer: 3510 = 2
  20. 20. Character Representation Ex: Find the binary ASCII and decimal ASCII values for the ‘&’ character. Rightmost Leftmost Three Bits Four Bits 000 001 010 011 100 101 110 111 0000 NUL DLE Space 0 @ P ` p 0001 SOH DC1 ! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EOT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN && 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y I y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS , < L l | 1101 CR GS - = M ] m } 1110 SO RS . > N ^ n ~ 1111 SI US / ? O _ o DEL
  21. 21. Character Representation ASCII Table From the chart: ‘&’ = 0100110 (binary ASCII value) Convert the binary value to decimal: 01001102 = 32 + 4 + 2 = 3810 Therefore: ‘&’ = 38 (decimal ASCII value)
  22. 22. Octal Numbering systems • Base: 8 • Digits: 0, 1, 2, 3, 4, 5, 6, 7 • Octal number: 12468 powers of : 84 83 82 81 80 decimal value: 4096 512 64 8 1 Octal number: 1 2 4 6
  23. 23. Octal to Decimal Conversion • To convert to base 10, beginning with the rightmost digit multiply each nth digit by 8(n-1) , and add all of the results together. Ex: 12468 positional powers of 8: 83 82 81 80 decimal positional value: 512 64 8 1 Octal number: 1 2 4 6 512 + 128 + 32 + 6 = 67810
  24. 24. Ex: 103528 positional powers of 8: 84 83 82 81 80 decimal positional value: Octal number:
  25. 25. The Division Method. Divide by 8 until you reach zero, and then collect the remainders in reverse. Ex 1: 433010 = 103528 8 ) 4330 Rem: 8 ) 541 2 8 ) 67 5 8 ) 8 3 8 ) 1 0 0 1
  26. 26. Ex 2: 81010 = 8 ) 810 Rem: 8 ) 8 ) 8 ) Answer: 81010 = 8
  27. 27. Hexadecimal Numbering systems • Base: 16 • Digits: 0, 1, 2, 3, 4, 5, 6, 7,8,9,A,B,C,D,E,F • Hexadecimal number: 1F416 powers of : 164 163 162 161 160 decimal value: 65536 4096 256 16 1 Hexadecimal number: 1 F 4
  28. 28. Four-bit Group Decimal Digit Hexadecimal Digit 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F
  29. 29. Hexa to Decimal Conversion • To convert to base 10, beginning with the rightmost digit multiply each nth digit by 16(n-1) , and add all of the results together. Ex: 1F416 positional powers of 16: 163 162 161 160 decimal positional value: 4096 256 16 1 Hexadecimal number: 1 F 4 256 + 240 + 4 = 50010
  30. 30. Ex: 7E16 positional powers of 16: 163 162 161 160 decimal positional value: Hexa number:
  31. 31. • The Division Method. Divide by 16 until you reach zero, and then collect the remainders in reverse. Ex 1: 12610 = 7E16 16) 126 Rem: 16) 7 14=E 0 7
  32. 32. Ex 2: 81010 = 16 ) 810 Rem: 16 ) 16 ) Answer: 81010 = 16
  33. 33. Binary to Octal Conversion • Since the maximum value represented in 3 bit is equal to: 23 – 1 = 7 • i.e. using 3 bits we can represent values from 0 –7 which are the digits of the Octal numbering system. • Thus, three binary digits can be converted to one octal digit and visa versa.
  34. 34. Three-bit Group Decimal Digit Octal Digit 000 0 0 001 1 1 010 2 2 011 3 3 100 4 4 101 5 5 110 6 6 111 7 7
  35. 35. Ex : Convert 7428 = 2 7 = 111 4 = 100 2 = 010 7428 = 111 100 0102
  36. 36. Ex : Convert 101001102 = 8 110 = 6 100 = 4 010 = 2 ( pad empty digits with 0) 101001102 = 2468
  37. 37. Four-bit Group Decimal Digit Hexadecimal Digit 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F
  38. 38. Ex : Convert 3D916 = 2 3 = 0011 D = 1101 9 = 1001 3D916 = 0011 1101 10012
  39. 39. Ex : Convert 101001102 = 16 0110 = 6 1010 = A 101001102 = A616
  40. 40. To convert between Octal to Hexadecimal numbering systems and visa versa convert from one system to binary first then convert from binary to the new numbering system
  41. 41. Ex : Convert E8A16 = 8 1110 1000 10102 111 010 001 010 (group by 3 bits) 7 2 1 2 E8A16 = 72178
  42. 42. Ex : Convert 7528 = 16 111 101 0102 (group by 4 bits) 0001 1110 1010 1 E A 7528 = 1EA16 Ex : Convert 7528 = 16 111 101 0102 (group by 4 bits) 0001 1110 1010 1 E A 7528 = 1EA16
  43. 43. Basic Structure of hardware and Software Topics: • Internal Hardware Features • External Hardware Features • Program Organization • Memory Organization • Basics of Assembly Programming • SK Editor • Using Debug
  44. 44. Objectives • To familiarize with the basic structure of hardware and software • To discuss programming model and memory organization • To discuss assembly programming using debug.
  45. 45. Basic Structure of Hardware and Software Hardware – is the physical part of the computer system Software - A program or set of instructions that controls the operation of a computer. Distinguished from the actual hardware of the computer.
  46. 46. Internal Hardware Features 1. Processor 2. Memory 3. Registers
  47. 47. External Hardware Features Computer’s Input/Output example: 1. Keyboard 2. Monitor 3. Disk 4. CD-ROM
  48. 48. Program Organization • Software consists of the operating system and various programs and data files stored on disk. • To execute (or run) a program: 1. the system copies it from external device into internal memory. 2. the processor executes the program instructions. 3. The registers handle the requested arithmetic, data movement, and addressing.
  49. 49. Memory Organization Bit – the fundamental building block of computer storage - off means 0, on means 1 Bytes – consists of eight bits for data and one bit for parity 0 0 0 0 0 0 0 0 1 Data bits parity According to the rule of parity, the number of bits that are on in each byte must always be odd.
  50. 50. Related Bytes • A program can treat a group of one or more related bytes as a unit of data, such as time or distance. A group of bytes that defines a particular value is commonly known as a data item or field. • Word – a 2-byte (16-bit) data item • Doubleword – A 4-byte (32-bit) data item • Quadword – An 8-byte (64-bit) data item
  51. 51. • Paragraph – a 16-byte (128 bit) area • Kilobyte (KB) – The number 210 equals, 1,024, which happens to be the value K, for kilobyte. • Megabyte (MB) – The number 220 equals 1,048,576, or 1 megabyte.
  52. 52. Basics of Assembly Programming Programming Language A programming language is a notation for expressing instructions to be carried out by the computer. It is a medium of communication between the human and the machine, and often between one human being and another.
  53. 53. Classification of programming Languages • High-Level Language – A high-level language is an English-like language that provides for more natural expression of algorithms. Examples: BASIC, Pascal, COBOL, & Prolog • Mid-level Language– It is a computer language which combines low-level efficiency with high-level language structure, abstraction and portability. Examples: C & MODULO-2 • Low-level Language – A low-level language is considered to be machine-oriented since it is closely follows the built in instruction set of the underlying computer. Examples: Assembly, machine languages.
  54. 54. Assembly Language • It is a low-level language that provides a kind of shorthand notation called mnemonic (memory aid) for the actual machine language instruction. It is a much easier to remember SUB AX<BX than 0010101111000011 which is the actual instruction code that the machine uses.
  55. 55. SideKick Editor • It is a text editor used in coding the source code. SideKick or SK editor can display the ASCII code for coding assembly language program.
  56. 56. Debug • BUG - It is a computer terminology for error or mistake in a program or computer system and the term debugging is a methodical process of finding and reducing the number of bugs or defects in a computer program. • DEBUGGER – It is a program tool that provides an environment for testing load modules. Load modules are executable programs
  57. 57. Registers Topics: 1. Register 2. General Purpose Registers 3. Pointer Registers 4. Segment Registers 5. Index Registers 6. Flag Registers
  58. 58. Objectives 1. To define what is a register. 2. Identify the types of registers. 3. Identify the function of each register.
  59. 59. Register Set Registers are fast memory, almost always connected to circuitry that allows various arithmetic, logical, control, and other manipulations, as well as possibly setting internal flags.
  60. 60. General-Purpose Registers AX - Accumulator Register mostly used for calculations and for input/output BX - Base Register Only register that can be used as an index AH AL AX EAX BH BL BX EBX
  61. 61. • CX - Count Register used for the loop instruction • DX - Data Register input/output and used by multiply and divide CH CL CX ECX DH DL DX EDX
  62. 62. Pointer Registers IP - Instruction Pointer 16-bit number that points to the offset of the next instruction SP - Stack Pointer 16-bit number that points to the offset that the stack is using BP - Base Pointer used to pass data to and from the stack
  63. 63. Segment Registers CS - Code Segment 16-bit number that points to the active code-segment DS - Data Segment 16-bit number that points to the active data-segment SS - Stack Segment 16-bit number that points to the active stack-segment ES - Extra Segment 16-bit number that points to the active extra-segment
  64. 64. Index Registers SI - Source Index used by string operations as source DI - Destination Index used by string operations as destination
  65. 65. Flag Registers Abr. Name bit nº Description OF Overflow Flag 11 indicates an overflow when set DF Direction Flag 10 used for string operations to check direction IF Interrupt Flag 9 if set, interrupt are enabled, else disabled TF Trap Flag 8 if set, CPU can work in single step mode SF Sign Flag 7 if set, resulting number of calculation is negative ZF Zero Flag 6 if set, resulting number of calculation is zero AF Auxiliary Carry 4 some sort of second carry flag PF Parity Flag 2 indicates even or odd parity CF Carry Flag 0 contains the left-most bit after calculations
  66. 66. System Operation Topics: • Disk Operating System • Debug • Loading of Debug from DOS • Loading SK through DOS
  67. 67. Objectives • To recall common DOS commands. • To discuss the procedures in starting and quitting Debug. • To familiarize with the Debug commands. • To know the rules in using debug commands.
  68. 68. System Operation Common Disk Operating System Commands 1. CLS - clear 5. DEL - delete 2. CD – change dir 6. REN - rename 3. DIR - directory 7. MD – make directory 4. COPY - copy 8. RD – remove directory
  69. 69. DEBUG Debug – is a software that is classified as debugger which is used for testing and debugging executable programs. - It displays all program code and data in hexadecimal format and any data that you enter into memory must also in hex form.
  70. 70. Advantages of Debug 1. It is free. 2. It is universally available. 3. It is simple to use. 4. It requires relatively little memory.
  71. 71. Debug Commands 1. Q (Quit) Finishes the Debug session and exits back to DOS environment Ex: -Q 2. H (Hexarithmetic) Shows the sum and difference of two 4-bit hexadecimal numbers, coded as H <hex value> <hex value> Ex: - 000C 0008
  72. 72. 3. R (Register) Allows you to display all registers and their values. It also shows the next instruction and permits you to change the value of a particular register. Ex: -R -R CX 4. E (Enter) Enables you to key in data or machine instructions into memory beginning at a specific location address. Ex: -E 0200
  73. 73. 5. D (Display or Dump) Displays the content of a portion memory in hex and ASCII forms starting with the given address. Ex: D 0200 6. A (Assemble) Allows you to create program in mnemonic or symbolic code. It also translates this assembly source statements that you create into machine code. Ex: -A 0100
  74. 74. 7. T (Trace) Runs the program in a single-step mode. It also displays the new values of the registers and the next instructions to be executed. Ex: -T 8. G (Go) Runs the program as a whole in memory and displays the output. Ex: -G
  75. 75. 9. U (Unassemble) Lists all the instructions contained in the program beginning at the given address. You can also specify the last address location. Ex: -U 0100 -U 0100 0109 10.N (Name) Gives a name to your program, coded as N <path><filename>. The base name of the filename must be eight characters long and the extension name is .COM Ex: N A:SAMPLE>COM
  76. 76. 11.W (Write) Saves the program onto the disk storage. Ex: -W
  77. 77. Rules of Debug Commands •It is not case sensitive •It assumes that all numbers given are in hexadecimal format. •You can enter a space only when it is needed to separate parameters of a particular command. •You should specify segments and offsets with a colon, in the form <segment>:<offset>.
  78. 78. Starting Debug 1. Key in the word debug and press enter. A hyphen (-) appears on the screen, that is the DEBUG prompt. DEBUG is now ready to accept commands. C:>debug -
  79. 79. Quitting and Closing Debug To close Debug and go back to DOS environment: type Q and press ENTER key. C:>debug -q C:>
  80. 80. Loading SK Through DOS • From the C:> prompt type SK then the SideKick main menu will appear. From the menu select Notepad or press F2. C:>sk
  81. 81. Basic Assembly Instructions Used in Debug 1. MOV (Move data) It copies and transfers data between two registers, or between an immediate data to a register. Format: MOV <register>,<register> MOV <register>, <immediate data>
  82. 82. Example: MOV AX,BX MOV CX, 5083 MOV CL,DL MOV BL,33 2. ADD (Add Data) It is used to get the sum of two registers or a register and an immediate data, and stores the result to the left most register.
  83. 83. Format: ADD <register>,<register> ADD <register>,<immediate data> Example: ADD CX,BX ADD AX,0308 ADD AL,BL ADD DH, 95 3. SUB (Subtract Data) It is used to get the difference of two registers or a register and an immediate data, and stores the result to the left most register.
  84. 84. Format: SUB <register>,<register> SUB <register>,<immediate data> Example: SUB CX,BX SUB AX,0308 SUB AL,BL SUB CL,95 3. MUL (Multiply Data) It is used to get the product of the given register and AX register, and stores the result to AX register. If the product is greater than 16 bits, the overflow is stored in DX register.
  85. 85. Format: MUL <register> Example: MUL CX 5. DIV (Divide Data) It is used to divide the value of a given register and AX register, and stores the quotient to AX and the remainder to DX registers respectively. Format: DIV <register> Example: DIV BX
  86. 86. 6. INC (Increment by one) It is used to increase the value of the register by one (1). Format: INC <register> Example: INC AX INC CH 7. DEC (Decrement by one) The opposite of INC, instead of increasing, it decreases the value of the register by one (1)
  87. 87. Format: DEC <register> Example: DEC AX DEC CH 8. LOOP (Loop Until Complete) It controls the execution of a program segment in a specified number of times. The CX register should contain a count value before starting the loop and automatically decrements by one (1). If CX is not equal to zero (0), it transfers to its operand address which points to the start of the loop; otherwise it drops through to the next instruction.
  88. 88. Format: LOOP <offset address> Example: LOOP 0108
  89. 89. Sample Debug Programs Displaying a smiley character on the screen. -A 0100 1358:0100 mov ah,02 ;request display character 1358:0102 mov dl,01 ;character to display 1358:0104 int 21 ;call interrupt service 1358:0106 int 20 ;end 13587:008 -G Program terminated normally -
  90. 90. Using control characters (0A – Line Feed and 0D – Carriage Return) in a screen display. -A 0100 1358:0100 MOV AH, 02 ;request display character 1358:0102 MOV DL,54 ;character ‘T’ 1358:0104 INT 21 ;call interrupt service 1358:0106 MOV DL, 0A ;line feed character 1358:0108 INT 21 ;call interrupt service 1358:010A MOV DL,0D ;carriage return character 1358:010C INT 21 ;call interrupt service 1358:010E MOV DL,49 ;character ‘I’ 1358:0110 INT 21 ;call interrupt service 1358:0112 MOV DL,0A ;line feed character 1358:0114 INT 21 ;call interrupt service 1358:0106 MOV DL,0D ;carriage return character 1358:0118 INT 21 ;call interrupt service 1358:011A MOV DL,50 ;character ‘P’ 1358:011C INT 21 ;call interrupt service 1358:011E INT 20 ;end
  91. 91. Displaying the same character (lowercase ‘z’) thirty times using loop on the screen. -A 0100 1358:0100 MOV CX,001E ;SETTING THE NUMBER OF LOOOPS 1358:0103 MOV AH,2 ;REQUEST DISPLAY CHARACTER 1358:0105 MOV DL,7A ;CHARCTER TO DISPLAY 1358:0107 INT 21 ;CALL INTERRUPT SERVICE 1358:0109 LOOP 0107 ;GO TO OFFSET 0107 1358:010B INT 20 ;END
  92. 92. Editor Topics: 1. Editor 2. Types of language translator 3. Advantages of assembly language
  93. 93. Objectives • To identify the different editors used in assembly language. • Differentiate the different types of language translator. • Enumerate the advantages of assembly language.
  94. 94. Editor Text Editors used in Turbo Assembler 1. Edit 2. Sidekick 3. Pilot
  95. 95. Turbo Assembler
  96. 96. Types of language Translator 1. Compiler – Is a translator program that transforms an entire source program composed of high/mid-level language statements into an object program consisting of machine language executable code. High/Mid-level language Source Program COMPILER Machine language Instruction
  97. 97. 2. Interpreter – processes the source program on a statement-by-statement basis. It translates high/mid-level language statements and then executes it one statement at a time. 2. Assembler – is a language translator that transforms assembly language to machine language instruction. High/Mid-level language Source Program INTERPRETER Machine language Instruction Assembly language Source Program ASSEMBLER Machine language Instruction
  98. 98. Assembly Language It is a low-level language that provides a kind of shorthand notation called mnemonic(memory aid) for the actual machine language instruction. It is much easier to remember SUB, AX, BX than 00101111000011 which is the actual instruction code that machine uses…
  99. 99. Advantages of Assembly language •It provides more control over handling particular hardware requirements. • It generates smaller, more compact executable modules. • It results in faster execution.
  100. 100. Tasm – Turbo Assembler T-link t - Turbo Assembler Linker Linker is a computer program for high, mid, and low levels of programming language that completes the process by converting the object code into executable machine code.
  101. 101. STRUCTURE OF AN ASSEMBLY LANGUAGE PROGRAM TITLE MYPROG.ASM DOSSEG . MODEL SMALL . STACK 0100h . DATA X DB “Hello everyone! $” . CODE MOV AX, @DATA MOV DS, AX MOV AH, 09h MOV DX, OFFSET X MOV AX, 4C00h INT 21h END
  102. 102. INSTRUCTIONS FOR INITIALIZING THE PROGRAM TITLE MYPROG.ASM DOSSEG . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX, @DATA MOV DS, AX
  103. 103. INSTRUCTIONS FOR CONCLUDING THE PROGRAM MOV AX, 4C00h INT 21h END
  104. 104. EXAMPLES OF ASSEMBLY LANGUAGE PROGRAMS • Displaying a character (uppercase ‘A’) on screen. TITLE MYPROG.ASM ; name of the program DOSSEG . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX, @DATA MOV DS, AX MOV AH, 02h ; request display character MOV DL, ‘A’ ; character to display MOV AX, 4C00h ; end INT 21h END
  105. 105. Displaying character (lowercase ‘b’) twice on screen. TITLE MYPROG.ASM ; name of the program DOSSEG . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX, @DATA MOV DS, AX MOV AH, 02h ; request display character MOV DL, ‘b’ ; character to display INT 21h ; call interrupt service INT 21h ; call interrupt service MOV AX, 4C00h ; end INT 21h END
  106. 106. • Using control characters (0A – Line Feed and 0D – Carriage return) in a screen display. TITLE MYPROG.ASM ; name of the program DOSSEG . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX, @DATA MOV DS, AX MOV AH, 02h ; request display character MOV DL, ‘T’ ; character to display INT 21h ; call interrupt service MOV DL, 0Ah ; line feed character INT 21h ; call interrupt service MOV DL, 0DH ; carriage return character INT 21h ; call interrupt service MOV DL, ‘I’ ; character ‘I’ INT 21h ; call interrupt service MOV DL, 0Ah ; line feed character INT 21h ; call interrupt service MOV DL, 0Dh ;carriage return character INT 21h ; call interrupt service MOV DL, ‘P’ ; character ‘P’ INT 21h ; call interrupt service MOV AX, 4C00h ; end INT 21h END
  107. 107. • Displaying different characters (‘A to ‘Z’) using LOOP on the same line. TITLE MYPROG.ASM DOSSEG . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX,@DATA MOV DS,AX MOV CX,001Ah ; setting the number of loops MOV AH,02h ; request display character MOV DL,‘A’ ; character ‘A’ B: INT 21h ; call interrupt service INC DL ; add to 1 to the value of DL LOOP B ; go to label B MOV AX,4C00h ; end INT 21h END
  108. 108. MIDTERM Week 7- Instructions And Directives Week 8 – Introduction to Assembly Language Week 9 – Using SK Editor Week 10 – Keyboard handling Week 11 – Screen Handling
  109. 109. Instructions And Directives Topics: • Instructions • Categories of Instructions • Directives • Types of Memory Model
  110. 110. Objectives To familiarize with the different instructions. To identify the categories of instructions To identify the different directives and their corresponding functions. To identify the different memory modes.
  111. 111. Instructions - These are statements that will be translated into machine language and executed by the computer.
  112. 112. Examples of instructions MOV ADD SUB MUL DIV INC DEC LOOP CMP
  113. 113. Categories of Instructions 1. DATA TRANSFER To move data in the memory and in the CPU registers. Format: Memory to register Register to register Register to stack Stack to register Syntax: Mov Destiny, Source Example: mov ax,bx Note: the list of symbolic instructions are for Intel processor family
  114. 114. There are two very important details to note about the mov instruction 1. There is no memory to memory move operation. 2. You cannot move immediate data into a segment register.
  115. 115. LDS: Load Data Segment Register LEA: Load Effective Address LES Load Extra Segment Register LODS: Load String LSS: Load stack Segment Register MOV: Move data MOVS: Move String
  116. 116. MOVSX: Move with sign-extend MOVZX: Move with Zero-extend STOS: Store string XCHG: Exchange XLAT: Translate
  117. 117. 2. ARITHMETIC Syntax: Adc destiny, source Example: Adc bh,bl ADD: add binary numbers ADC: Add with carry DEC: Decrement by 1 INC: Increment by 1 DIV: Unsigned Divide IDIV: Signed (integer) divide MUL: Unsigned multiply IMUL: Signed multiply SUB: Subtract binary values SBB: Subtract with borrow XADD: exchange and add
  118. 118. 3. COMPARISON Syntax: CMP destiny, source Example: cmp ax,bx CMP: compare CMPSn: Compare string CMPXCHG: Compare and exchange
  119. 119. 4. LOGICAL OPERATIONS Syntax: AND destiny,source Example: And ax,bx AND: Logical AND NOT: Logical NOT OR: Logical OR XOR: Exclusive XOR 5. INPUT/OUTPUT Syntax: OUT destiny,source Example: in ax,dx IN: Input byte or word INSn: Input string OUT: Output byte or word OUTSn: Output string
  120. 120. 6. JUMP(conditional) Syntax: ja label Example: ja start JA: Jump if Above JAE: Jump if above/equal JB: Jump if below JBE: Jump if below/equal JE: Jump if Equal JG: Jump if Greater JGE: Jump if Greater/Equal JL: Jump if Less JNE: Jump if Not Equal
  121. 121. JNG: Jump if Not Greater JNGE: Jump if Not Greater/Equal JNL: Jump if Not Less JNLE: Jump if Not Less/Equal JNZ: Jump if Not Zero JLE: Jump if Less/Equal JNA: Jump if Not Above JNB: Jump if Not Below JNBE: Jump if Not Below/Equal JZ: Jump if Zero
  122. 122. JC: Jump if Carry JCXZ: Jump if CX is Zero JNO: Jump if No Overflow JO: Jump if Overflow JNP: Jump if No Parity JP/JPO: Jump if Parity Odd JPE: Jump if Parity Even JS: Jump if Sign JNS: Jump if No Sign
  123. 123. 7. JUMP (Unconditional) CALL: Call a procedure INT: Interrupt IRET: Interrupt Return JMP: Unconditional Jump RET: Return RETN/RETF: Return Near/Return Far
  124. 124. 8. LOOPING Syntax: loop label Example: loop a LOOP: Loop until complete LOOPE: Loop While Equal LOOPZ: Loop While Zero LOOPNE: Loop While Not Equal LOOPNZ: Loop While Not Zero
  125. 125. 9. STACK INSTRUCTIONS Syntax: pop destiny Example: pop ax POP: Retrieve the content of the Stack PUSH: Store the content of the Stack POPF: Pop Flags off the Stack PUSHF: Push Flags onto Stack
  126. 126. Directives - These are statements that give information to the assembler. - Sometimes called pseudo-ops (short term for pseudo operations and pronounced as”su-da- op”). - Unlike instructions, directives are not translated is not machine language but they are necessary for your program to assemble properly.
  127. 127. Examples of Directives TITLE DOSSEG .MODEL .STACK .DATA .CODE
  128. 128. Most Common Directives 1. TITLE – It creates a title (up to 60 characters) of a source listing. Format: Example: TITLE PROGRAM1.ASM TITLE <TEXT>
  129. 129. 2. DOSSEG – It tells the assembler to ignore all other requests and to adopt the DOS segment sequence – stack, data and code. Format: Example: DOSSEG DOSSEG
  130. 130. 3. .MODEL – It specifies and initializes the memory model before defining any segment. Format: Example: .MODEL TINY .MODEL SMALL .MODEL MEDIUM .MODEL SMALL <MEMORY-MODEL>
  131. 131. Types of Memory Model MEMORY MODEL NO. OF DATA SEGMENT NO. OF CODE SEGMENT Tiny 0 0 Small 1 1 Medium 1 More than 1 Compact More than 1 1 Large More than 1 More than 1
  132. 132. 4. .STACK – It defines the size of the stack. The default size is 1,024 bytes which you can overrule. Format: Example: .STACK 0100h .STACK <SIZE>
  133. 133. 5. .DATA – It defines and marks the beginning of data segment. Format: Example: .DATA .DATA
  134. 134. 6. .CODE – It defines and marks the code segment which consists of a set of instructions. Format: Example: .CODE .CODE
  135. 135. 7. END – It is placed at the last line of the source code. Format: Example: END END
  136. 136. 8. For Defining Data DIRECTIVE LENGTH DESCRIPTION DB 1 Define Byte DW 2 Define Word DD 4 Define Doubleword DF 6 Define Farword DQ 8 Define Quadword DT 10 Define Tenbytes
  137. 137. Introduction to Assembly Language Topics: • Requirements for Coding Assembly language • Reserved Words • Identifiers
  138. 138. Objectives • To know the requirements in coding assembly language • To identify the different reserved words. • To know the categories of reserved words • To identify the two types of identifiers. • To know the general guidelines in coding assembly program.
  139. 139. Introduction to Assembly Language Using Turbo Assembler
  140. 140. Requirements for Coding Assembly language Comment -Is a part of a program that is ignored by the assembler. Though Optional, the use of comments throughout a program can improve its readability and clarity especially in assembly language where the purpose of a set of instructions is often ambiguous. -It begins with a semicolon(;)
  141. 141. Ways to include comment • Any Statement whose first non-blank character is a semicolon. Example: ; This program displays Good Day! •At the end of an instruction. Example: MOV AX, 8053h ; initializes the value of ax to 8053h
  142. 142. Reserved Words These are words in which the assembler assigns a special meaning and it cannot be used as identifiers. They are reserved for their own purpose to be used only under special condition.
  143. 143. Categories of Reserved Words •Instructions •Directives •Operators •Pre-defined Symbols
  144. 144. Identifiers An identifier is a user-defined name (Variable) that you apply to an item in your program that you expect to reference.
  145. 145. Types of Identifiers •Name – It refers to the address of a data item. Example: x db 0 msg db “Aloha! &” •Label – It refers to the address of an instruction or procedure. Example: mov dl, 41h A: int 21h add dl, 20h int 21h sub dl, 20h loop A
  146. 146. Rules in Constructing Valid Identifier •It must be letters (A..Z, a..z), numbers (0-9) &/or special characters like underscore(_), question mark(?) and at sign (@). •* It must always start with a letter. •It must not use reserved words. •It must not exceed to 31 characters. Examples of valid identifiers: neym u2u2 num_1 msg8 Examples of Invalid identifiers: title num-1 4ever F-4
  147. 147. Statement An assembly language statement program consists of a set of statements. The two types of statements are instructions and directives.
  148. 148. General Guidelines for coding statement •A statement may begin anywhere on the line •Each line can only contain one statement •Unlike C language, assembly is not case sensitive. You can use either upper or lowercase letters. •Take note that statements can have two (2) operands, a single and even none at all. Examples: ADD AX,BX ;uses 2 operands DEC CX ; uses single operand RET ; no operand
  149. 149. String -is used for directive data such as person’s name or simply a message. It must end with dollar ($) symbol and defined in double quotation marks (“ ”). DB is the conventional format for defining string of any length. Example: Neym db “Mary’s Internet Café $”
  150. 150. Numeric Constant -They are used to define arithmetic values and memory addresses. It is identified with a radix specifier such as d for decimal, b for binary and h for hexadecimal. Example: Msg db “Good day!”, 0ah, 0dh, “$” Is the same with Msg db “Good day!”, 10d, 13d “$” Msg db “Good day!”, 00001010b, 00001101b, “$”
  151. 151. Using SideKick Assembling and Linking Procedures Topics: • Using SideKick • Creating Assembly Language • Structure of Assembly language • Assembling and Linking Assembly Language
  152. 152. Objectives • To familiarize with the SideKick Environment as text editor. • To know the procedures in invoking Sidekick and quitting from the SideKick window. • To know the procedures in creating and saving assembly program. • To know the procedures in assembling, linking and running assembly program.
  153. 153. Using Sidekick as Editor Sidekick - is an editor used for DOS programming like assembly language.
  154. 154. Creating Assembly Language Program Step 1: Open the text editor. C:tasm>SK Step2: Type the name of the program with .asm as file extension name. Example: c:program1.asm Step3: You may now start typing your source code.
  155. 155. Exiting from the Sidekick Window * Press escape (esc )key to go back to DOS environment.
  156. 156. Structure of an Assembly Language Program TITLE PROGRAM1.ASM DOSSEG .MODEL SMALL .STACK .DATA X DB “HELLO THERE!$” .CODE MOV AX, @DATA MOV DS,AX MOV AH,O9H MOV DX,OFFSET X INT 21H MOV AX,4C00H INT 21H END Initializes the program Concludes the program Body of the program
  157. 157. Assembling and Linking Procedures Assembling: From the c:tasm> prompt type the following the filename and its extension name then press enter key. Example: c:tasm>program1.asm
  158. 158. Linking: After a successful assembly (I.e., no errors encountered), type tlink, press space bar, filename and its extension name then press enter key Example: c:tasm>tlink program1.obj
  159. 159. Executing: To run the program, just type the filename only at c:tasm>. Example: c:tasm>program1
  160. 160. To recapitulate the overall process: 1. Open the text editor. 2. Save the program. 3. Start keying your program. 4. Exit the editor and go back to DOS environment by pressing the Esc key. 5. Assemble the program by typing tasm filename.asm. 6. If there are no errors, do the next step otherwise, repeat steps 1-5 and rectify the program. 7. Link the object file by typing tlink filename.obj & press enter key. 8. Type filename and press enter key to run the program.
  161. 161. KEYBOARD HANDLING Topics: Interrupt and Service Character Output Character Input String Display String Input
  162. 162. Objectives • To identify the function/service number used to input/output character. • To identify the function/service number used to input/output string. • To identify the corresponding interrupt used in character and string input/output.
  163. 163. Interrupt – execute commands Service – set the command Interrupt and Service
  164. 164. Character Output Int 21h / 02 This function display one character on the screen. AH = 02h DL = holds the character to display on the screen.
  165. 165. Example: . MODEL SMALL . STACK 0100h . DATA . CODE MOV AX, @DATA MOV DS, AX MOV AH, 02h ; request display character MOV DL, ‘j’ ; character to display INT 21h ; call interrupt service INT 21h ; call interrupt service MOV AX, 4C00h ; end INT 21h END
  166. 166. Character Input Int 21h / 1 – Keyboard input This function waits for you to type a character on the keyboard. It echoes the character to the screen and returns the ASCII character in the AL register. AH = 01 AL = character read from the keyboard
  167. 167. Example: .model small .stack .data .code mov ah, 01h ; request for character input int 21h
  168. 168. Character Input Int 21h / 7 – keyboard input without echo This function reads a character from a keyboard but doesn’t display the character on the screen and wait for control break. AL = character read from the keyboard
  169. 169. Character Input Int 21h / 8 – keyboard input without echo This function works the same way with service 7 but do not respond to control break. AL = character read from the keyboard
  170. 170. Example:.MODEL SMALL .STACK 0100H .DATA MSG DB “INPUT ANY CHARACTER: $” MSG2 DB 0AH, 0DH, “The character you type is: $” x db 0 .CODE MOV AX,@DATA MOV DS, AX MOV AH,09H MOV DX,OFFSET MSG1 INT 21H MOV AH,01H ;REQUEST FOR CHARACTER INPUT INT 21H MOV X,AL MOV AH,09H MOV DX,OFFSET MSG2 INT 21H MOV AH,02H MOV DL,X INT 21H MOV AX,4C00H INT 21H
  171. 171. String Display Int 21h / 09 This function displays the string pointed by the DS:DX pair of registers. The end of the string should be marked with the $ sign. DS:DX points to address of the string
  172. 172. String Display Int 21h / 09h Display string in the data area followed by a dollar sign ($ or 24h) delimiter, which the operation uses to end the display. Example: Nick_name db “Your nickname: $“ ;display string
  173. 173. Example fragment of a program MOV AH,09H ;request display LEA DX, NICK_NAME ;load address of prompt INT 21H ;call interrupt service
  174. 174. String Input Int 21h / 0A This function reads the string from the keyboard. DS:DX points to address of the string
  175. 175. Example: . MODEL SMALL . STACK 0100h . DATA NEYM DB 08h, ?, 08h DUP (“$”) Q DB “What is your name? $” A1 DB 0Ah, 0Dh, “What a nice name ! $” A2 db 0Ah, 0Dh, “I’m glad to meet you ! $” . CODE MOV AX, @DATA MOV DS, AX MOV AH, 09h MOV DX, OFFSET Q MOV AH, 0Ah ; request for string input MOV DX, OFFSET NEYM ; load address INT 21h MOV AH, 09h MOV DX, OFFSET A1 INT 21h MOV AH, 09h MOV DX, OFFSET NEYM+2 INT 21h MOV DX, OFFSET A2 INT 21h MOV AX, 4C00h INT 21h END
  176. 176. DISPLAYING THE INPUT STRING The following instructions display the input string starting at the third memory address. MOV AH, 09h ; request for string output MOV DX, OFFSET X+2 ; LOAD ADDRESS INT 21h
  177. 177. Sample program that demonstrates string and displays it again on screen. . MODEL SMALL . STACK 0100h . DATA NEYM DB 08h, ?, 08h DUP (“$”) Q DB “What is your name? $” A1 DB 0Ah, 0Dh, “What a nice name ! $” A2 db 0Ah, 0Dh, “I’m glad to meet you ! $” . CODE MOV AX, @DATA MOV DS, AX MOV AH, 09h MOV DX, OFFSET Q MOV AH, 0Ah ; request for string input MOV DX, OFFSET NEYM ; load address INT 21h MOV AH, 09h MOV DX, OFFSET A1 INT 21h MOV AH, 09h MOV DX, OFFSET NEYM+2 INT 21h MOV DX, OFFSET A2 INT 21h MOV AX, 4C00h INT 21h END
  178. 178. Screen Handling Topics: • The Monitor • GRAPHICS
  179. 179. Objectives • To identify the instructions and directives used for screen handling applications • To create an assembly program with the application of graphics.
  180. 180. THE MONITOR • A typical video screen has eighty (80) columns numbered from 0 to 79 and twenty five (25) rows numbered from o to 24. The columns and rows provide a grid of addressable locations at any one of which the cursor can be set. The table below shows the examples of a cursor locations
  181. 181. Screen Location COLUMN ROW COLUMN ROW In hexadecimal value In hexadecimal value Upper Left Corner 0 0 00H 00H Upper Right Corner 79 0 4FH 00H Center of Screen 40 12 28H 0CH Lower Left Corner 0 24 00H 18H Lower Right Corner 79 24 4FH 18H Screen Features
  182. 182. Clearing the Screen in Assembly Approach Int 10h / 06h Handles the process of clearing the screen and scrolling. AH = Function in 06H AL = number of lines to scroll, or 00H for the full screen BH = attribute value (color blinking etc.) CX = starting row column DX = ending row column
  183. 183. sample code shows how to create the clear screen: MOV AX,0600h ;AH=06h (Scroll), AL=00h (Full Screen) MOV BH,07h ;0 (Black Background), 7 (White Text Color) MOV CX,0000h ;CH=00h (Row), CL=00h (Column) MOV DX,184Fh ;DH=18h (Row), DL=4Fh (Column) INT 10h ;Call interrupt service
  184. 184. SETTING THE CURSOR POSITION Int 10h / 02 Used to set the cursor position. AH = 02 MOV AH, 02h ; request to set the cursor position MOV BH, 002h ; page number 0 MOV DH, 0Ah ; Row = 10 in decimal MOV DL, 08h ; Column = 8 in decimal INT 10h ; call interrupt service
  185. 185. Alphanumeric
  186. 186. DISPLAYING THE STRING ‘FLOWERS’ AT THE CENTER OF THE SCREEN .MODEL SMALL .STACK 0100h .DATA A DB “FLOWERS$” .CODE MOV AX,@DATA MOV DS,AX MOV AX,0600h ; AH=00h (Scroll0, AL=00h (Full Screen) MOV BH,07h ; 0 (Black Background), 7 (White Text Color) MOV CX,0000h ; CH=00h (Row), CL 00h (Column) MOV DX,184Fh ; DH=18h (Row), DL=4Fh (Column) INT 10h ; Call interrupt Service MOV AH,02h ; request to set the cursor position MOV BH,00h ; page number 0 MOV DH,0Ch ; Row = 12 in decimal MOV DL,24h ; Column = 36 in decimal INT 10h ; Call interrupt service MOV AH,09H ; request display string LEA DX,A ; load address of the string MOV AX, 4C00h INT 21h END
  187. 187. Graphics TEXT AND BACKGROUND COLORS BACKGROUND COLOR TEXT COLOR HEX CODE MEANING BL R G B I R G B 0 0 1 0 1 0 0 0 28 Green Background Color Gray Text Color 1 0 1 0 1 0 0 0 0A8 Green Background Color Gray Black Color Blinking 0 1 1 0 0 0 0 1 61 Brown Background Color Blue Text Color 1 1 1 0 0 0 0 1 0E1 Brown Background Color Blue Text Color Blinking
  188. 188. DESIGNING PROGRAMS USING PROCEDURE Procedures make it possible to break a large program down into smaller pieces so that each piece can be shown to work correctly without the reference to any of the others.
  189. 189. DEFINING A PROCEDURE Every procedure is defined using PROC and ENDP directives. Since these are directives, they don’t generate machine language instructions; rather, PROC and ENDP are directions to the assembler, making the beginning and end of a procedure. In assembly, the first declared procedure in the program is considered the major calling procedure.
  190. 190. Format & Example • Format: • • <procedure-name> PROC • <instruction-1> • <instruction-2> • … • <instruction-n> • <procedure-name> ENDP • Example: • • MAIN PROC • CALL P1 • CALL P2 • CALL QUITPROGRAM • MAIN ENDP
  191. 191. CALLING PROCEDURE To call a procedure Format: CALL <procedure-name>
  192. 192. FINALS Week 13 – File and Disk handling Week 14 – Sample Programs in Assembly Language Week 15 – Math Operation Week 16 – In-line Assembly Week 17 – Case Study
  193. 193. FILE AND DISK HANDLING Topics: •Common DOS File Handle Services •File Attributes •Modes of Accessing Files
  194. 194. Objectives: • To identify and familiarize with the different services for file and disk handling. • To create a program using the different services in file and disk handling. • To identify the different file attributes • To apply the different modes of accessing files.
  195. 195. Common DOS File Handle Services
  196. 196. INT 21H / 39H Create Subdirectory This service creates a subdirectory as does the system command MKDIR. To use it, load DX with the address of an ASCIIZ string containing the drive and directory pathname. AH = 39H DS:DX = points to ASCIIZ If CX = 1, AX has error
  197. 197. Example fragment of a program: ASCstrg DB ‘n:pathname’, 00H ;ASCIIZ string … MOV AH,39H ;request create subdirectory LEA DX,ASCstrg ;Address of ASCIIZ string (DS:DX) INT 21H
  198. 198. Remove Subdirectory This service deletes a subdirectory, just as does the system command RMDIR. Note that you cannot delete the current (active) directory or a subdirectory containing files. Load DX with the address of an ASCIIZ string containing the drive and directory pathname. AH = 3AH DS:DX = points to ASCIIZ string IF CX = 1, AX has error INT 21H / 3AH
  199. 199. Example fragment of a program: ASCstrg DB ‘n:pathname’,00H ;ASCIIZ string … MOV AH,3AH ;request delete subdirectory LEA DX,ASCStrg ;address of ASCIIZ (DS:DX) INT 21H ;call interrupt service
  200. 200. INT 21H / 56H Rename File or Directory This service name a file or directory from within a program. Load DX with the address of an ASCIIZ string containing the old drive, path and name of the file or directory to be renamed. Load DI (combined as ES:DI) with the address of an ASCIIZ string containing the new drive, path and name, with no wild cards.
  201. 201. Drive numbers, if used, must be the same in both strings. Because the paths need not be the same, the operation can both rename a file and move it to another directory on the same drive.
  202. 202. Example fragment of a program: Oldstring db ‘n:oldpatholdname’,00h Newstring db ‘n:newpath]newname’,00h … MOV AH,56H ;request rename file/directory LEA DX,Oldstring ;DS:DX LEA DI,Newstring ;ES:DI
  203. 203. INT 21H / 5BH Create a new File This service creates a file only if the named file does not already exist; otherwise it is identical to function 3CH (create file). You could use function 5BH whenever you don’t want to overwrite an existing file.
  204. 204. INT 21H / 3CH Create File AH = 3CH DS:DX = points to ASCIIZ string CX = attribute
  205. 205. File Attributes BIT ATTRIBUTE 0 Read only file 1 Hidden file 2 System file 3 Volume label 4 Subdirectory 5 Archive file
  206. 206. INT 21H / 43H Get/Set File Attribute You can use this operation either to get or set a file attribute in the directory. This operation requires the address of an ASCIIZ string containing the drive, path and filename for the requested file. (Or use the default directory if no path is given.)
  207. 207. Example fragment of a program: ASCstrg db ‘n:pathname’,00H ;ASCIIZ string … MOV AH,43H ;request MOV AL,00H ;get attribute LEA DX,ASCstrg ;ASCIIZ string (DS:DX) INT 21H ;call interrupt service
  208. 208. INT 21H / 3BH Change Directory This service changes directory. AH = 3BH DS:DX = points to ASCIIZ string If CX = 1, AX has error
  209. 209. Modes for Accessing Files Access mode Means 0 Read Only 1 Write Only 2 Both Read and Write
  210. 210. INT 21H / 3DH Open File This function opens a file. AH = 3DH DS:DX = points to ASCIIZ string AL = MODE
  211. 211. INT 21H / 3EH Close File This service closes a file. AH = 3EH BX = file handle If CX = 1, AX has error
  212. 212. INT 21H / 41H Delete File This service deletes a file (but not read- only) from within a program. Load the address in DX of an ASCIIZ string containing the device path and filename, with no wild cards references.
  213. 213. Example fragment of a program: ASCstrg db ‘n:pathname’,00H ;ASCIIZ string … MOV AH,41H ;request delete file LEA DX,ASCSTRG ;address of ASCIIZ string (DS:DX) INT 21H ;call interrupt service
  214. 214. Sample Programs In Assembly Language Topic: • Designing programs using procedure • Basic Stack Instruction • Loop Instruction
  215. 215. Objectives: • Create an assembly program with procedures. • Create an assembly program using stack instruction. • Create an assembly program using loop instruction.
  216. 216. DESIGNING PROGRAMS USING PROCEDURE • DEFINING A PROCEDURE Every procedure is defined using PROC and ENDP directives. Since these are directives, they don’t generate machine language instructions; rather, PROC and ENDP are directions to the assembler, making the beginning and end of a procedure. In assembly, the first declared procedure in the program is considered the major calling procedure.
  217. 217. Format: <procedure-name> PROC <instruction-1> <instruction-2> … <instruction-n> <procedure-name> ENDP Example: MAIN PROC CALL P1 CALL P2 CALL QUITPROGRAM MAIN ENDP
  218. 218. CALLING PROCEDURE To call a procedure Format: CALL <procedure-name>
  219. 219. Example: .model small .code org 100h start: jmp main x db “assembly$” y db “language$” z db “program$” main proc near mov dx,offset x call proc1 call proc2 mov dx,offset y call proc 1 call proc2 mov dx,offset z call proc1 call proc2 main endp
  220. 220. proc2 proc near mov ah,2 mov dl,13 int 21h mov dl,10 int 21h ret proc2 endp proc1 proc near mov ah,9 int 21h ret proc1 endp end start
  221. 221. Basic Stack Instruction Push Instruction puts/places data onto the stack segment Pop Instruction takes out data from the stack
  222. 222. Format: • push reg16 • pop reg16 • push reg32 (3) • pop reg32 (3) • push segreg • pop segreg (except CS) push memory • pop memory
  223. 223. • The first two instructions push and pop a 16 bit general purpose register. This is a compact (one byte) version designed specifically for registers
  224. 224. • The second pair of instructions push or pop an 80386 32 bit general purpose register.
  225. 225. • The third pair of push/pop instructions let you push or pop an 80x86 segment register.
  226. 226. • The fourth pair of push/pop instructions allow you to push or pop the contents of a memory location.
  227. 227. Example: .model small .code org 100h start: mov cx,5 x: mov ah,1 int 21h add al,20h push ax loop x
  228. 228. mov ah,2 mov dl,13 int 21h mov dl,10 int 21h mov cx, 5 y: pop dx int 21h loop y int 20h end start
  229. 229. Loop Instruction Loop allows a part of the program to be repeated for several times, the number of loops depend on the value of the CX register. - each time the loop is executed, CX is decremented and the looping stops when CX becomes zero.
  230. 230. Format: LOOP label Example: LOOP start This will cause the program flow to go to the label start for CX times.
  231. 231. Example: .model small .code org 100h start:mov cx,20 mov ah,2 mov dl, ‘j’ z: int 21h loop z end start
  232. 232. Math Operation • BCD • ASCII
  233. 233. Objectives: • To create a program using addition, subtraction, multiplication and division of Binary Coded Decimal. • To create a program that converts the addition, subtraction of ASCII codes to BCD. • To identify the instructions and directives for addition, subtraction of BCD.
  234. 234. BCD • binary coded decimals Binary coded decimal (BCD) is a method for implementing lossless decimal arithmetic (including decimal fractions) on a binary computer. The most obvious uses involve money amounts where round-off error from using binary approximations is unacceptable. Some early computers used BCD exclusively. •
  235. 235. • Decimal digits (0-9) can be encoded in a nibble (half a byte), with some left over bit patterns (hexadecimal A-F). In BCD operations, the processor performs ordinary binary computations, then adjusts the result to conform to BCD. • For example, if you add the binary number 5 (bit pattern 0101) to binary number 6 (bit pattern 0110), you get the binary result of 11 (bit pattern 1011, or hexadecimal B). With BCD arithmetic, the processor would adjust the result to make it into a valid BCD result (which in this case would be bit pattern 0001 0001).
  236. 236. • BCD arithmetic includes BCD addition, BCD subtraction, BCD multiplication, BCD division, and BCD negate. The Intel 80x86 series uses a two step approach for BCD arithmetic. Instead of having separate BCD instructions, the normal binary addition and subtraction instructions are used, then hardware instructions are used to adjust the results to correct BCD results. • There are instructions for both packed and unpacked adjustments.
  237. 237. • The advantage of this approach is greater flexibility (more addressing modes and choices of arithmetic operations because of the use of regular binary integer instructions in the first step). • The disadvantage of this approach is that it is slower and takes more memory.
  238. 238. ASCII • American Standard Character Information Interchange. • Refer to ASCII Table
  239. 239. Adding ASCII Numbers Consider the effect of the following three examples of adding ASCII numbers: Ex. 1 35H Ex.2 38H Ex.3 39H +32H 34H +39H Total 67H 6CH 72H
  240. 240. Example 1. The sum of 35h and 32h is 67h. AAA checks the rightmost digit (7). Because it is not between A and F, nor is the AF flag set, AAA clears the leftmost digit (6) to 0.
  241. 241. Example 2. The sum of 38H and 34 H is 6CH. Because the right most digit (C) is between A and F, AAA performs the following: AH AL 00 0C Adds 6 to AL 00 72 Adds 1 to H 01 72 Clears leftmost digit of AL 01 02 (sum = 12)
  242. 242. Example 3. The sum of 39H and 39H is 72H. Although the rightmost digit (2) is not between A and F, the AF flag is set because of the carry into the tens position. AAA performs the following: AH AL 00 72 Adds 6 to AL 00 78 Adds 1 to AH 01 78 Clears leftmost digit of AL 01 08 (sum = 18)
  243. 243. Subtracting ASCII NUMBERS Example 1. ASCIIvalue1 contains 39h and ASCII value2 contains 39h. Subtracts ASCIIvalue2 (35h) from ASCIIvalue1 (39h). AAS does not need to make an adjustment because the rightmost hex digit is less than the hex A. AX AF CF MOV AL,ASCVALUE1 ; 0039 SUB AL,ASCVALUE2 ; 0004 0 0 AAS ; 0004 0 0 OR AL,30h ; 0034
  244. 244. Example 2. Subtracts ASCIIvalue1 (39h) from ASCII value2 (35h). Because the rightmost digit of the result is hex C, AAS subtracts 6 from AL, subtracts 1 from AH, and sets the AF and CF flags: AX AF CF MOV AL,ASCVALUE2 ; 0035 SUB AL,ASCVALUE1 ; 00FC 1 1 AAS ; FFO6 1 1 The answer, which should be -4, is FF06h, its ten’s complement; that is, decimal -10 + 6 = -4.
  245. 245. Multiplying ASCII Numbers INSTRUCTION COMMENT AX CL . . . ; Initial values 0035 39 AND CL, 0FH ; Convert CL to 09 0035 09 AND AL, 0FH ; Convert CL to 05 0005 09 MUL CL ; Multiply AL by CL 002D 09 AAM ; Convert AX to unpacked BCD OR AX 3030H ; Convert AX to ASCII 3435
  246. 246. Dividing ASCII Numbers INSTRUCTION COMMENT AX CL . . . ; Initial values 3238 37 AND CL,0FH ; Convert to unpacked BCD 3238 07 AND AX,0FH0FH ; Convert to unpacked BCD 0208 AAD ; Convert to binary 00IC DIV CL ; Divide by 7 0004
  247. 247. CONVERTING BINARY DATA TO ASCII FORMAT • The following example converts binary number 0DF1H to ASCII format: ASCVALUE DB 4 DUP (‘ ‘) ; Data Items BINVALUE DW 0DF1H . . . MOV CX, 0010 ; Division factor LEA SI, ASCVALUE ;Address of SCVALUE MOV AX, BINALUE ; Get binary amount
  248. 248. L20: CMP AX,CX ; Value < 10? JB L30 ; yes, exit XOR DX,DX ; Clear upper quotient DIV CX ; Divided by 10 OR DL,30H MOV [SI],DL ; Store ASCII character DEC SI JMP L20 L30: OR AL,30H ;Store last quotient MOV [SI], AL ; as ASCII character
  249. 249. C20: mov al,[SI] ;get a byte Shr al,04 ;shift off right hex digit Lea bx,xlatab ;set table address Xlat ; translate hex Stosb ;store AL in DISPAREA Mov al,[SI] ;get other half of byte And al,0fh ;clear left hex digit Xlat ;translate hex Stosb ;store AL in DISPAREA Inc SI ; incr address of IOBUFFER Lea bx,IOBUFFER+512 ; Cmp SI,BX ;end of IOBUFFER? Jb c20 ;if no, repeat Lea bp,disparea ;if yes, display Mov cx,1024 ;hex characters Call D10DISPLAY ret C10CONVRT endp
  250. 250. ; display data: ; - - - - - - - - - ;uses AX, BX, DX D10DISPLAY proc near ;BP, CX set on entry MOV AX,1301 ;request display MOV BX,0016H ;page:attribute MOV DX,0500H ;row:column RET D10DISPLAY end p End A10MAIN
  251. 251. In-Line Assembly Topic: • Comparison of C Language to Assembly
  252. 252. Objective: • To construct a program with the integration of Pascal and C language programming.
  253. 253. Comparison of C Language To Assembly Language
  254. 254. If..Else Construct A conditional statement of C language in the form: If <condition> { <statement-1>; <statement-2>; . . . <statement-n>; } Else { <statement-1>; <statement-2>; . . . <statement-n>; }
  255. 255. Can be implemented by assembly language of the form: <cmp instruction> <conditional jump instruction> <label-1> <instruction-1> <instruction-2> … <instruction-n> <jump instruction> <label-2>
  256. 256. Comparison C language Assembly language If (ax == 0) { cx = cx – ax; ax = ax + 1; } Else { Cx = cx – 8; } CMP AX,0000H JNZ ACTION1 SUB CX,AX INC AX JMP NEXT ACTION1: SUB CX,0008H NEXT:
  257. 257. Do..While Construct A do..while loop similar to C language: do { <statement-1>; <<statement-2>; … <statement-n>; } while <condition>;
  258. 258. Can be implemented by assembly in the form: <label>: <instruction-1> <instruction-2> … <instruction-n> <cmp instruction> <conditional jump instruction> <label>
  259. 259. Comparison C language Assembly language ax = 5; do { ax = ax – 1; bx = bx + 1; } while (ax >= 0); MOV AX,0005H HERE: DEC AX INC BX CMP AX JNZ HERE
  260. 260. While Construct Similarly, C language while loop: While <condition> { <statement-1>; <statement-2>; … <statement-n>; }
  261. 261. Is roughly equivalent to the assembly language structure: <label-1>: <cmp instruction> <conditional jump instruction> <label> <instruction-1> <instruction-2> . . . <instruction-n> jmp <label-1> <label-2>: <instruction-1> <instruction-2> . . . <instruction-n>
  262. 262. Comparison C language Assembly language while (ax <= 10) { ax = ax + bx; bx = bx + 1; } START: CMP AX,000AH JGE NEXT ADD AX,BX INC BX JMP START NEXT: ADD CX,AX
  263. 263. Final Case Study Design a problem that will utilize any of the constructs if-else, do- while and while. Please don’t forget to check the complexity and eventual completeness of your output.
  264. 264. Objective: • To develop programs that apply the whole concepts of assembly programming.
  265. 265. Final Examination and Case Presentation
  266. 266. ENDEND

×