You are on page 1of 16

Introduction with Debug The Debug Facility Debug is a program that is a part of the MS-DOS that allows the

e user to: 1. Enter an assembly language program into the PC; 2. Execute it; 3. Examine the results that the program produces; and 4. If necessary, debug any errors in its operation. The command name is debug. The DEBUG prompt is a hyphen or an underscore; command is written after this prompt. Opening the Debug Facility 1. Click on Start to get to DEBUG. 2. On the Start Menu, select All Programs. 3. On the next Menu, select Accessories. 4. Under Accessories, select Command Prompt. You should get the DOS prompt line C:\users\<user name> 5. Type DEBUG on the DOS prompt line and the Debug prompt - should appear. 6. Type in ? for a listing of the Debug commands you can enter. DEBUG Debug is a method of looking at portions of your computer and writing assembly code to perform certain tasks on your computer. MS-DOS 2.x - 4.x uses debug.com MS-DOS 5.x and above uses debug.exe Possible issues If during the time you are typing the debug routine you receive ^error this is and indication that you have incorrectly typed something within the line just completed. You can type the line again without causing any problems with the routine. Entering debug Debug has been included in every version of MS-DOS as well as Windows. When running any of the debug routines it is recommended that if you have Windows that you exit or shut down to get into a real DOS prompt (unless you're running Windows ME, Windows 2000, Windows XP, or later versions). The debug command is an external command and is available in the below Microsoft operating systems. o All Versions of MS-DOS, Windows 95, Windows 98, Windows ME, Windows NT, Windows 2000, Windows XP DEBUG Program The DEBUG program is used for testing and debugging executable programs which include to: 1. viewing the content of the main memory (MM) 2. enter programs in memory 3. trace the execution of a program DEBUG also provides a single-step mode, which allows you to execute a program one instruction at a time, so that you can view the effect of each instruction on memory locations and registers. DEBUG Commands - The following are some DEBUG commands : A : Assemble symbolic instructions into machine code D : Display the contents of an area of memory in hex format E : Enter data into memory, beginning at a specific location H : Perform hexadecimal arithmetic N : Name a program Q : Quit the DEBUG session R : Display the contents of one or more registers in hex format T : Trace the execution of one instruction U : Unassemble (or disassemble) machine code into symbolic code Rules of DEBUG Commands DEBUG does not distinguish between lowercase and uppercase letters.

DEBUG assumes that all numbers are in hexadecimal format Spaces in commands are used only to separate parameters Segments and offset are specified with a colon, in the form segment: offset Example: To display the content in segment FE0016 beginning from the first byte of the segment, in the DEBUG mode, type: D FE00:0 Or d fe00:0 first byte of the segment segment fe00 display command, can use lowercase or uppercase letter

the command d or D (D Display) will display 8 rows of data and each row contains 16 bytes (32 digit hex) which adds up to a total of 128 bytes (8 rows), beginning from the address given Machine Language Example: Using Immediate Data (Immediate Mode) the DEBUG program can also be used to enter the program code into the memory and trace its execution Below is an example of a program in machine language (written in hexadecimal) and assembly language (symbolic code) together with description about the instructions Machine Instruction Symbolic Code (Assembly Language) Explanation B82301 MOV AX,0123 Move value 0123H to AX 052500 ADD AX,0025 Add value 0025H to AX 8BD8 MOV BX,AX Move contents of AX to BX 03D8 ADD BX,AX Add contents of AX to BX 8BCB MOV CX,BX Move contents of BX to CX 2BC8 SUB CX,AX Subtract contents of AX from CX 2BC0 SUB AX,AX Subtract AX from AX EBEE JMP 100 Go back to the start The first and second instructions in the program above use immediate addressing mode where the real data value is in the address field MOV AX, 0123 ADD AX, 002 Other instructions use register addressing mode (general purpose registers) To enter instructions in machine language into the memory (code segment), the e or E command is used followed by the address of the segment code at 100 (beginning addess of instructions in a code segment (100H = 256B)) - refer to Table 1 below. (E Enter) To trace the execution of the program, use r or R first to view the content in the CPU registers followed by the command t or T. - refer to Table 2 below. (R Register; T Trace) -e CS:100 B8 23 01 05 25 00 -e CS:106 8B D8 03 D8 8B CB -e CS:10C 2B C8 2B C0 EB EE
DS=2090 ES=2090 2090:010A 8BCB -t AX=0148 BX=0290 DS=2090 ES=2090 2090:010A 8BCB -t AX=0148 BX=0290 DS=2090 ES=2090 2090:010C 2BC8 -t AX=0148 BX=0290 DS=2090 ES=2090 2090:010E 2BC0 -t AX=0000 BX=0290 DS=2090 ES=2090 2090:0110 EBEE SS=2090 CS=2090 IP=010A NV UP EI PL NZ AC PE NC MOV CX,BX CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=010A NV UP EI PL NZ AC PE NC MOV CX,BX CX=0290 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=010C NV UP EI PL NZ AC PE NC SUB CX,AX CX=0148 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=010E NV UP EI PL NZ AC PE NC SUB AX,AX CX=0148 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=0110 NV UP EI PL ZR NA PE NC JMP 0100

-r AX=0000 BX=0000 DS=2090 ES=2090 2090:0100 B82301 -t AX=0123 BX=0000 DS=2090 ES=2090 2090:0103 052500 -t AX=0148 BX=0000 DS=2090 ES=2090 2090:0106 8BD8 -t AX=0148 BX=0148 DS=2090 ES=2090 2090:0108 03D8 -t AX=0148 BX=0290

CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=0100 NV UP EI PL NZ NA PO NC MOV AX,0123 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=0103 NV UP EI PL NZ NA PO NC ADD AX,0025 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=0106 NV UP EI PL NZ NA PE NC MOV BX,AX CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000 SS=2090 CS=2090 IP=0108 NV UP EI PL NZ NA PE NC ADD BX,AX CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000

to view the instruction that is entered in the code segment, use the d command followed by cs:100 (100 is the word byte starting address that is allowed in the code segment) - refer Table 3 below
-d cs:100 2090:0100 2090:0110 2090:0120 2090:0130 2090:0140 2090:0150 2090:0160 2090:0170 8B 23 01 05 25 00 8B D8-03 D8 8B CB 2B C8 2B C0 .#..%.......+.+. EB EE E8 59 00 5F 5E 59-5B 58 5A 1F 34 00 7F 20 ...Y._^Y[Xz.4.. D5 E2 00 74 F7 1E 0E 1F-BE D5 E2 E8 98 02 2E A1 ...t............ 2F E7 BB 40 00 BA 01 00 33 FF CD 21 1F 72 0B 8B /..@....3..!.r.. D8 B0 FF 86 47 18 A2 18-00 C3 0E 1F E8 D2 00 3D ....G..........= 41 00 74 07 0B FF 74 06-BA 39 82 E9 CD FC 2E C6 A.t...t..9...... 06 67 E1 01 BA 69 E1 2E-A3 69 E1 E9 BD FC 80 3E .g...i...i.....> E7 04 00 75 03 E9 9A 00-BE E7 04 E8 48 02 80 3E ...u........H..>

Enter the program above into the memory: -A 100 2090 : 0100 2090 : 0102 2090 : 0104 2090 : 0106 2090 : 0108 MOV CL, 42 MOV DL, 2A ADD CL, DL JMP 100

To view machine code for the assembly language entered, use the u or U command. (U Un-assemble) -U 100 107 2090 : 0100 B142 MOV CL, 42 2090 : 0102 B22A MOV DL, 2A 2090 : 0104 00D1 ADD CL, DL 2090 : 0106 EBF8 JMP 0100 the machine code for the instruction entered To execute the above program, as usual, use the r or R command followed by the T or t command.

Simple Program Loops with Debug Storing and Running Programs It would not seem practical to type an entire program each time it is needed. Thus, to avoid this, it is possible to store a program on the disk. The steps to create a program are: 1. Assemble the program 4. Name the program 2. Obtain the length of the program 5. Save the program 3. Change the content of CX with the size 6. Quit debug of the program 7. Run the program Example Program Step 1. Assemble the program the command A. Enter the A command in the debug prompt. Type the program: MOV AX,3808 JNZ 109 MOV BX,0A POP DX MOV CX,0 ADD DL,30 MOV DX,0 MOV AH,02 DIV BX INT 21H PUSH DX LOOP 115 INC CX MOV AH,4C CMP AX,0 INT 21H Step 2. Size of the program Obtain the length of the program using the H command. First result shows us the addition of the parameters and the second is the subtraction. Type the command at the debug prompt: H 0123 0100 The first parameter is the ending address of the program and the second parameter is the beginning address of the program.

Step 3. Register the size The R CX command allows us to change the content of the CX register to the value we obtained from the size of the file with h. Type the command at the debug prompt: R CX CX 000 : 0023 Step 4. Name the program The command N allows you to name the program. Type the command at the debug prompt: -n test.com Step 5. Write the program The command W writes the program on the disk, indicating how many bytes it wrote. Type the command at the debug prompt: -w Step 6. Quit Debug Quit debug by the typing the command Q at the debug prompt. This lets you go back to the command prompt. Step 7. Run the program The program can be executed by the typing its name at the command prompt. Type the name of the program at the command prompt: c:\ test.com Analyzing the program What answer was displayed on the screen? Here again is the whole program: MOV AX,3808 JNZ 109 MOV BX,0A POP DX MOV CX,0 ADD DL,30 MOV DX,0 MOV AH,02 DIV BX INT 21H PUSH DX LOOP 115 INC CX MOV AH,4C CMP AX,0 INT 21H MOV Instruction A data transfer instruction that facilitate the movement of data either between registers or between a register and main memory. Format: MOV D, S Note: D is the destination operand while S is the source. Example: MOV AX,3808 (moves the value 3808H in the register AX) MOV BX,0A (moves the value 0AH in the register BX) MOV CX,0 (moves the value 0 in the register CX) MOV DX,0 (moves the value 0 in the register DX) DIV Instruction Divides the number stored in AX by the number stored in the operand; the quotient is written in AX and the remainder is written in DX. Format: DIV O Note: O is the operand which will serve as the divisor of AX. Example: DIV BX (Divides AX by the value of BX) PUSH Instruction Moves the value of S to the source operand to the top of the stack memory.

Format: PUSH S Example: PUSH DX (saves the value of DX at the top of the stack memory) INC Instruction Adds 1 to the destination operand. Format: INC D Example: INC CX (increment CX by 1) CMP Instruction Subtracts the source operand from the destination operand. It then discards the result but it updates the values of all the conditional flags. Format: CMP D, S Example: CMP AX,0 (compare the value of AX to 0 by subtracting the two values and updates the flags) JNZ Instruction Jump if Not Zero. Lets you jump to the specified address if the zero flag is reset. If the zero is not set then the program continues to the next command below. Format: JNZ A Note: A is the address where to jump. Example: JNZ 109 (jump to address 109 in the program if the zero flag is set) POP Instruction Retrieves a word data from the top of the stack memory and save it into the destination. Format: POP D Example: POP DX (unload a data from the top of the stack memory and save it to DX) ADD Instruction The add instruction adds the source operand to the destination operand and then places the result in the destination operand. Format: ADD D, S Example: ADD DL,30 (adds the value of DL by 30 and then saves the result at the DL register) Interrupt Commands Most of the general functions and services offered by DOS are implemented through this interrupt. INT 21h in the 512's implementation of DOS Plus 2.1 provides 77 official functions, two of which are non-functional and return with no action. Within this range some calls have sub functions which further extend the range of operations. In all calls, on entry AH defines the function. In all cases the general programming technique is to set AH to the function pointer, set up the required register contents (and the memory block if necessary) then to issue the call by the assembly code INT instruction Service 02 Interrupt 21h Function 2 - Character output Action: Outputs a character to the screen. On entry: AH = 02h DL = 8 bit data (usually ASCII character) Example: MOV AH,02 (outputs the character INT 21H defined by the ASCII Code saved in register DX) ASCII Code Loop Instruction The LOOP instruction decrements CX by 1 and transfers control to the target location if CX is not 0 otherwise, the instructi on following the LOOP is executed. Format: LOOP A

Example: LOOP 115 (decrement CX by 1 and loop back to address 115 in the program if CX is not zero if not the program continues on the next command below) Service 4ch Interrupt 21h Function 4Ch - Terminate program with return code Action: Terminates execution of a program with return to COMMAND.COM or a calling routine, passing back a return code. On entry: AH = 4Ch Example: MOV AH,4CH (terminates the program) INT 21H SIMPLE PROGRAM LOOPS with PROGRAMMING CONSTRUCTS 1. Sequence A sequence of ASM statements that does not contain any instruction that alters program are said to be sequential. These statement are strictly executed one by one. Example: mov ah, 01h mov dl, al mov dx, offset msg int 21h mov ah, 02h mov ah, o9h mov char, al int 21h int 21h 2. Selection The selection constructs consists of conditionals and branching. Conditionals allow the testing of particular conditions while a program is running. These conditions usually involve comparing values in registers and/or variables or the test for some hardware status. Whenever conditionals are evaluated, a branching action is said to occur in the operation Branching In assembly language, appears as jump instructions: JMP JE JNE JG JGE JL JLE JA JAE JB JBE Jump instructions indicate to which location in a program will control transfer. In other words, they determine which instruction location gets executed next. Jump Instructions The location where control transfers to is indicated by a label (identifier followed by a colon). Except for the jmp, the other forms of the instruction are usually preceded by a compare (cmp) instruction and will be performed only when the compare instruction is satisfied. Branching can be classified into high-level constructs that are present in high-level languages. Force Jump Consider the following code examples: jmp SOMEWHERE mov ax, bx SOMEWHERE: mov dx, cx The statements above demonstrate a force jump. When the jmp instruction is encountered, program control immediately transfers to the label SOMEWHERE:. Hence, in this example mov ax, bx will never be executed. IF-THEN Example (in Pascal): if AL>DL then CX:=0 In ASM cmp al,dl TRUE: jg TRUE mov cx,0 jmp FALSE FALSE:

Notice the strong logical resemblance of the ASM version with the high-level language (Pascal) example. This shows the preservation of logic in converting a high-level equivalent. Below is also an ASM equivalent of the previous example -- yet reverse logic. cmp al,dl TRUE: jle FALSE mov cx,0 FALSE: IF-THEN ELSE Example: if AL>DL then CX:=0 else CX:=256 In ASM cmp al,dl jmp END_IF jle ELSE_PART ELSE_PART: THEN_PART: mov cx,0ffh mov cx,0h END_IF: An IF_THEN_ELSE form of branching provides alternative actions for both TRUE and FALSE values of the conditional. Take note of the jmp END_IF statement -- this serves to skip the ELSE_PART when control falls in the IF_PART. 3. Compound Conditions Compound conditions arise when the conditional involves the evaluation of more than a single logical expression. The mixing of AND, OR, and NOT logic often complicates programming. Assembly language supports compound conditions through a series of compare and jump pairs. Below is an example of implementing ANDed conditionals. if AL>DL and BH<DH then CX:=0 else CX:=256 Example in ASM cmp al,dl mov cx,0 jle ELSE_PART jmp END_IF cmp bh,dh ELSE_PART: jae ELSE_PART mov cx,0ffh THEN_PART: END_IF: The following implements an OR'ed conditional if AL>DL or BH<DH mov cx,0 then CX:=0 jmp END_IF else CX:= 256 ELSE_PART: mov cx,0ffh cmp al,dl END_IF: jg THEN_PART cmp bh,dh From here, it should not be jle ELSE_PART too confusing to implement THEN_PART: mixed AND,OR, and NOT conditionals. CASE The CASE statement provides appropriate actions to be performed per specific value of a variable. It can also have a catch-all ELSE portion. The following is an implementation of a CASE statement. Example in Pascal: case AL of 0: AX:=10; 1: BX:=11; 2: CX:=0; end;

Example in ASM cmp al,0 je ZERO cmp al,1 je ONE cmp al,5 je FIVE jmp ELSE_PART

ONE: mov ax,08h jmp END_CASE FIVE: mov ax,och jmp END_CASE ELSE_PART: xor ax,ax END_CASE:

ZERO:

mov ax,0ah jmp END_CASE 3. Iteration Iteration provides means to execute instructions more than once depending on some condition. The iteration is also referred as loops. The statements whose execution are repeated are called the body of the loop. FOR LOOP When the condition of the loop refers to the number of items such instructions are to be performed, then the loop is specifically called an iteration. In some high-level language, iterations are implemented in what is known as a FOR LOOP. Example of FOR LOOP In Pascal: for CX:=1 TO 10 do write(msg); In ASM: mov cx,0ah FOR_LOOP: mov dx,offset msg mov ah,09h int 21h loop FOR_LOOP The execution of the loop body may depend on some particular condition other than the given number of times the loop body is to be executed. In this case, the evaluation of such general condition may appear in two locations before or after the body of the loop. WHILE_DO Loop When this condition is evaluated before performing any statement in the loop body, the WHILE_LOOP loop construct is used. The following is an ASM rendition of the WHILE_DO loop. Example: AX:=0; while AX<=10 do begin AX:=AX + 1; BX:=BX*2; end; Example in ASM xor ax,ax Notice that in the WHILE_DO WHILE_LOOP: loop, the loop body may never cmp ax,0ah get executed in case the jge END_WHILE condition is initially evaluated inc ax as TRUE (using reverse logic). shl bx,1 jmp WHILE_loop END_WHILE:

REPEAT_UNTIL Loop If the condition at the end of the loop body, the REPEAT_UNTIL construct is used. Below is an example of implementing a REPEAT_UNTIL loop. Example in Pascal: repeat char:=readkey until char:=ESCAPE; Example in ASM REPEAT_LOOP: mov ah,07h int 21h mov char,al cmp al,1bh jne REPEAT_LOOP Notice that in a REPEAT_UNTIL loop, the loop body is executed at least once. Bit Manipulation Central Processing Unit A CPU is similar to a calculator, only much more powerful. The main function of the CPU is to perform arithmetic and logical operations on data taken from memory or on information entered through some device, such as a keyboard, scanner, or joystick. The CPU is controlled by a list of software instructions, called a computer program. Software instructions entering the CPU originate in some form of memory storage device such as a hard disk, floppy disk, CD-ROM, or magnetic tape. Memory These instructions are then pass into the computers main random access memory (RAM), where each instruction is given a unique address, or memory location. As a program is executed, data flow from RAM through an interface unit or wires called the bus, which connects the CPU to RAM. The data are then decoded by a processing unit called the instruction decoder that interprets and implements software instructions. Arithmetic Logic Unit From the instruction decoder, the data pass to the Arithmetic Logic Unit (ALU), which performs calculations and comparisons. Data may be stored by the ALU in temporary memory locations called registers where it may be retrieved quickly. The ALU performs specific operations such as addition, multiplication, and conditional tests on the data in its registers, sending the resulting data back to the RAM or storing it in another register for further use. Program Counter During this process, a unit called the program counter keeps track of each successive instruction to make sure that the program instructions are followed by the CPU in the correct order. The program counter in the CPU usually advances sequentially through the instructions. However, special instructions called branch or jump instructions allow the CPU to abruptly shift to an instruction location out of sequence. Branching Statements These branches are either unconditional or conditional. An unconditional branch tests the result of a previous operation to see if the branch should be taken. For example, a branch might be taken only if the result of a previous subtraction produced a negative result. Data that are tested for conditional branching are stored in special locations in the CPU called flags. Logic Instructions The AND Instruction Logically ANDs the source and the destination operands and stored the result in the destination operand. Format: AND D, S Action: D [D] + [S] Destination Source Example register register AND BX,CX register MM AND DX,[BP +SP]

MM register MM

register immediate immediate

AND BETA,CX AND BX,0015H AND byte ptr BETA, 12H

Pointers 1. The AND instruction does not allow both source and destination operands to be memory locations (no memory to memory addition). Therefore, the instruction AND SET, FILE is invalid. 2. Both source and destination operands should be of the same data size. The instruction AND AH,BX is therefore invalid. 3. The destination operand cannot be immediate data. Therefore, the instruction AND 1800H, CX is invalid. 4. CF and OF are always 0 after execution while AF is undefined. 5. As with the MOV instruction, if the destination operand is a memory location and the source operand is immediate data, the prefix byte ptr or word ptr should appear after the AND instruction to denote the data size of the immediate operand. Example MOV AL,05H MOV BL,FEH AND AL,BL The OR Instruction The OR instruction logically Ors the source and the destination operands and stores the result in the destination operand. Format: OR D, S Action: D [D] + [S] Destination Source Example register register OR BX,CX register MM OR DX,[BP +SI] MM register OR BETA,CX register immediate OR BX,0015H MM immediate OR byte ptr BETA, 12H Pointers 1. The OR instruction does not allow both source and destination operands to be memory locations (no memory to memory addition). Therefore, the instruction OR SET, FILE is invalid. 2. Both source and destination operands should be of the same data size. The instruction OR AH,BX is therefore invalid. 3. The destination operand cannot be immediate data. Therefore, the instruction OR 1800H, CX is invalid. 4. CF and OF are always 0 after execution while AF is undefined. 5. As with the MOV instruction, if the destination operand is a memory location and the source operand is immediate data, the prefix byte ptr or word ptr should appear after the OR instruction to denote the data size of the immediate operand. Example MOV CL,05H MOV DL,80H OR CL,DL The XOR Instruction The XOR instruction logically XOrs the source and the destination operands and stores the result in the destination operand. Format: XOR D, S Action: D [D] + [S] Destination Source Example register register XOR BX,CX register MM XOR DX,[BP +SI] MM register XOR BETA,CX register immediate XOR BX,0015H

MM immediate XOR byte ptr BETA, 12H Pointers 1. The XOR instruction does not allow both source and destination operands to be memory locations (no memory to memory addition). Therefore, the instruction OR SET, FILE is invalid. 2. Both source and destination operands should be of the same data size. The instruction XOR AH,BX is therefore invalid. 3. The destination operand cannot be immediate data. Therefore, the instruction XOR 1800H, CX is invalid. 4. CF and OF are always 0 after execution while AF is undefined. 5. As with the MOV instruction, if the destination operand is a memory location and the source operand is immediate data, the prefix byte ptr or word ptr should appear after the OR instruction to denote the data size of the immediate operand. Example MOV AL,15H MOV BL,ABH XOR AL,BL The NOT Instruction The NOT instruction performs a 1s complement on the operand. Format: NOT D Action: D [D] Destination Example register NOT AX MM NOT byte ptr BETA Pointers 1. The destination operand cannot be immediate data. Thus, the instruction NOT 1800H is invalid. 2. Flags are not affected. 3. If the destination operand is a memory location, the prefix byte ptr or word ptr should appear after the NOT instruction to denote the data size of the destination operand. Example MOV BX,D32BH MOV CX,1056H NOT BX NOT CX Developing an Assembly Language Program Text Editor Software designers create new programs by using special applications programs, often called utility programs or development programs. A programmer uses another type of program called a text editor to write the new program in a special notation called a programming language. Example of Text Editor Notepad Wordpad Word Edit.com To access edit.com, type edit on the command prompt and press enter. To exit from edit.com click exit from the file menu. Source Code With the text editor, the programmer creates a text file, which is an ordered list of instructions, also called the program source file. The individual instructions that make up the program source fie are called source code. It has a file extension of .asm.

Object Code At this point, a special applications program translates the source code into machine language, or object codea format that the operating system will recognize as a proper program and be able to execute. It has a file extension of .obj. Source Code to Object code 3 types of applications programs translate from source code to object code: compilers, interpreters, and assemblers. The three operate differently and on different types of programming languages, but they serve the same purpose of translating from a programming language into machine language. Compilers A compiler translates text files written in high-level programming languagesuch as Fortran, C, or Pascalfrom source code to the object code all at once. This differs from the approach taken by interpreted languages such as BASIC, APL and LISP, in which a program translated into object code statement by statement as each instruction is executed. Interpreters The advantage of interpreted languages is that they can begin executing the program immediately instead of having to wait for all the source code to be compiled. Changes can also be made to the program fairly quickly without having to wait for it to be compiled again. Disadvantages of Interpreted and Compiled Language The disadvantage of interpreted languages is that they are slow to execute, since the entire program must be translated one instruction at a time, each time the program is run. On the other hand, compiled languages are compiled only once and thus can be executed by the computer much more quickly than interpreted languages. Assemblers Another type of translator is the assembler, which is used for programs or parts of programs written in assembly language. In assembly language, a single statement can usually be translated into a single instruction of machine language. Example of Assemblers MicroASM TASM Stands for Turbo Assembler from Borland. To use TASM, move to the directory where tasm is located and type tasm followed by the filename and . asm as the file extension of the program to be assembled. Example: D:\tasm>tasm filename.asm Debugger Programs seldom work correctly the first time, so a program called a debugger is often used to help find problems called bugs. Debugging programs usually detect an event in the executing program and point the programmer back to the origin of the event in the program code. 2 Kinds of Bugs Program Error These are bugs that makes the program unrunnable. You cannot continue linking the program without first correcting the bugs. User Error These are bugs that cannot be seen by the assembler instead the user sees it by knowing that the objectives of the program are not met. Error Messages vs. Warning Messages Error Messages needs to be fixed before a program can continue. It must not be taken for granted because the program is halted in the position where the error messages are seen. Warning Messages may be ignored for the first time since it cannot affect the execution of your program. However, you must also address warning messages especially when the program needs to be implemented for end-users.

Linkers

Programs are often written as a set of smaller pieces, with each piece representing some aspect of the overall application program. After each piece has been compiled separately, a program called a linker combines all of the translated pieces into a single executable program. Example of Linkers TLINK Turbo Linker from Borland. To use tlink, move first to the directory where tlink is located and type tlink together with the filename of the program being link without the extension name. Example: D:\tasm>tlink filename Running the Program Finally, if your program is free form bugs you can now run your program by typing its filename in the command prompt without extension name. Example: D:\tasm>filename Elements of an Assembly Language Program An assembly language program is written according to the following structure and includes the following assembler directives: title OPTIONAL PROGRAM NAME .model SIZE .stack ADDRESS .data .code end Memory Model Size Assembler directives that defines the memory model to use in the program. The memory model determines the size of the code, stack and data segments of the program. Memory Model Size of Code and Data Tiny Small Medium Compact Large Code and Data no more than 64KB Code and Data segments must be no more than 64KB each Code can be more than 64KB, Data still limited to no more than 64KB Code limited to no more than 64KB, Data can be more than 64KB. Code and Data can each be more than 64KB, no array can be larger than 64KB

Huge Code and Data can each be more than 64KB, arrays can be larger than 64KB Stack Directive Assembler directive that reserves a memory space for program instructions in the stack. Directive is .stack ADDRESS Should be declared even if program itself doesnt use stack needed for subroutine calling and possibly passing parameters. May be needed to temporarily save registers or variable content with PUSH and POP commands. Data Segment Directive Assembler directive that reserves a memory space for constraints and variables. Directive is .data Different data types for different size of memory includes: DB define byte (8 bits) DW define word (16 bits) DD define double word (32 bits) DQ define quad word (64 bits) Code Segment Directive Assembler directive that defines the program instructions. Directive is .code for code segment.

The body of your program resides here. It includes the different instructions, labels, subroutines and interrupts you used inside the program you create. End of Program Assembler directive that finishes the assembler program. Directive is end. Tells the assembler that this is the end of the program. First Assembly Language EDITOR A computer program used to create text files or to make changes to an existing text file. An editor offers some of the capabilities of a word processor but is usually less powerful. It can, for example, be used to delete, insert, and serach for text, but it might not have such features as wordwrap (the ability to break lines automatically) and text formatting (italics and so forth). Full Screen Editors Some editors, generally called text or full-screen editors, enable the user to move the cursor through the document with direction keys and related keystrokes. Other editors, called line editors, view a document as a related keystrokes. Other editors, called line editors, view a document as a series of numbered lines; to change text with a line editor, the user specifies the number of the line containing the desired text. The MS-DOS editor Edlin is an example of a line editor. Assembly Language Assembly Language a type of low-level computer programming language in which each statement corresponds directly to a single machine instruction. Assembly Language a type of low-level computer programming in which each statement corresponds directly to a single machine instruction. Assembly languages are thus specific to a given processor. Translator After writing an assembly language program, the programmer must use the assembler specific to the microprocessor to translate the assembly language into machine code. Assembly language provides precise control of the computer, but assembly language written for one type of computer must be rewritten to operate on another type. Reasons for using Assembly Language Assembly language might be used instead of a high-level language for any of three major reasons: speed, control, and preference. Programs written in assembly language usually run faster than those generated by a compiler; use of assembly language lets a programmer interact directly with the hardware (processor, memory, display, and input/output ports). Compiler Compiler is a computer program that translates source code, instructions in a program written by a software engineer, into object code, those same instructions written in a language the computers central processing unit (CPU) can read and interpret. Software engineers write source code using high level programming languages that people can understand. Computers cannot directly execute source code, but need a compiler to translate these instructions into a low level language called machine code. Functions of Compilers Compilers collect and reorganize (compile) all the instructions in a given set of source code to produce object code. Object code is often the same as or similar to a computers machine-code. If the object code is the same as the machine language, the computer can run the program immediately after the compiler produces its translation. Machine Language If the object code is not in machine language, other programssuch as assemblers, binders, linkers, and loaders finish the translation. Most programming languagessuch as C, C++, and Fortranuse compilers, but somesuch as BASIC and LISPuse interpreters.

Interpreter An interpreter analyzes and executes each line of source code one-by-one. Interpreters produce initial results faster than compilers, but the source code must be re-interpreted with every use and interpreted languages are usually not as sophisticated as compiler languages. Most computer languages use different versions of compilers for different types of computers or operating systems, so one language may have different compilers for personal computers (PC) and Apple Macintosh computers. Many different manufacturers often produce versions of the same programming language, so compilers for a language may vary language may vary between manufacturers. Consumer software programs are compiled and translated into machine language before they are sold. Some manufacturers provide source code, but usually only programmers find the source code useful. Thus programs bought off the shelf can be executed, but usually their source code cannot be read or modified. First Assembly Language .model small .stack 100h .data design db Congratulations ,32 db 121,111,117,114,32,102,105,114,115,116,32 db 65,115,101,109,98,108,121,32 db 76,97,110,103,117,97,103,101,32 db 112,114,111,103,114,97,109,32,105,115,32 db 119,111,114,107,105,110,103,33,13,10,$ program db 13,10,20 dup(32),You are Gre,97,t!!!,36 Discussion .model small means that Code and data segments must be no more than 64KB each .stack 100h directive that reserves a memory space for program instructions in the .data directive that reserves a memory space for constants and variables design db Congratulations ,32 define byte (8 bits), congratulations 32 is the decimal ascii code for SP (space) db 119,111,114,107,105,110,103,33,13,10,$ $ is the terminator per line of code program db 13,10,20 dup(32),You are Gre,97,t!!!,36 13, CR (carriage return). 10, LF (line feed) 20 dup(32), duplicate 20 times the ascii code 32 (space) .code push ds sub ax,ax push ax mov ax,@data mov ds,ax these sequence of instructions at the beginning of a program used to assign the data segment. may be replaced by the following directive: .startup mov ah,09h mov dx,offset design int 21h This function is used to display a string of characters ended with a $ sign. The following code displays the string DESIGN defined as in the data part.

mov dx,offset program int 21h What will be the output on the screen? mov ah,4ch int 21h end can be replaced by the command below and .exit end

returns to DOS.

DOS Interrupts Interrupts 20h through 3fh are reserved for DOS operations, as described in the following sections. INT 20H Terminate Program Ends execution of a .com program, restores addresses for control + break and critical errors, flushes register buffers, and returns control to the DOS. INT 21H The main DOS operation which requires a function in the AH and is described. INT 10H Service 0 Set Screen Mode Input AH = 0 AL = Mode Mode Display Lines Adapters (in AL) 3 80x25 CGA,EGA,VGA D 320X200 EGA,VGA 12 640x480 VGA INT 10H Service 1 Set Cursor Type Input AH=1 CH=Cursor Start Line CL=Cursor End Line Output New Cursor INT 10H Service 2 Set Cursor Position Input DH,DL = Row, Column BH=Page Number AH=2 Note: DH,DL=0,0 = Upper Left Output Cursor position changed.

You might also like