Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CH/S7CS/Aug., 2002 Assembly Language (Ch. 20-21 Ray Bradley, Ch. 28 C. S. French) ** Refer to the notes on machine codes. (p. 10-15 Basic Machine Organisation) Low-level languages are or resembles languages in which each instruction corresponds to . Each computer manufacturer normally devises a low-level language that corresponds closely to the particular machine language used by that manufacturer. i.e. Assembly language. Common features of assembly languages. i. are used in place of the opcode of machine codes. ii. are frequently used instead of actual machine address. The assembly language must be translated, or assemblied, into machine language by an ____________ before use. A program written in assembly language is called an assembly program, which is translated to a program in machine codes called the object program. Although each microprocessor has its own assembly language, and each has a different instruction set, all assembly languages have many things in common, e.g. fields of instruction. Instruction Format There are usually four fields in a typical assembly language instruction, although not all of them must be used. A typical arrangement of these four fields is as follows: Label field, opcode (mnemonic), operand (address), and comments. The assembly language that will be studied is Turbo Assembler 4.0. Here is an example of Turbo Assembler program. .MODEL SMALL .STACK 100H .DATA TimePrompt GoodMorningMessage GoodAfternoonMessage DefaultMessage .CODE start: mov mov mov mov int mov int or cmp je cmp je mov jmp DB DB DB DB 'Is it after 12 noon (Y/N)?$' 13,10,'Good morning, World!',13,10,'$' 13,10,'Good afternoon, World!',13,10,'$' 13,10,'Greetings, World!',10,13,'$' ax,@data ds,ax dx,OFFSET TimePrompt ah,9 21h ah,1 21h al,20h al,'y' IsAfternoon al,'n' IsMorning ;set DS to point to the data ;point to the time prompt ;DOS: print string ;display the time prompt ;DOS: get character ;get a single-character response ;force character to lower case ;typed Y for afternoon? dx,OFFSET DefaultMessage DisplayGreeting ;typed N for morning? ASSEMBLY LANGUAGE ;default greeting page 1 CH/S7CS/Aug., 2002 IsAfternoon: mov dx,OFFSET GoodAfternoonMessage jmp DisplayGreeting ;afternoon greeting IsMorning: mov dx,OFFSET GoodMorningMessage ;before noon greeting DisplayGreeting: mov ah,9 int 21h mov ah,4ch mov al,0 int 21h ;DOS: print string ;display the appropriate greeting ;DOS: terminate program ;return code will be 0 ;terminate the program END start Commenting the program i. Comments at end of the line, starting with a semicolon (;). e.g. ii. mov [bx], al ;store the modified character. The COMMENT directive a. COMMENT ignores all text from the first delimiter character and the line containing the text before the occurrence of the next delimiter. e.g. COMMENT * stuff here * Extending the line Turbo Assembler provides the line continuation character (\). Use this character at the end of your line, and Turbo Assembler will ignore all characters that follow it on the same line. e.g. DB 'Hello out there\ you guys' Assigning values to symbols i. EQU The EQU directive defines a string, alias, or numeric equate. e.g. Man EQU 'Human Being' ii. = The = directive defines only a numeric equate. e.g. PI = 3.1416. Ending the programming Use the END directive to mark the end of your source file. ASSEMBLY LANGUAGE page 2 CH/S7CS/Aug., 2002 Expressions and Symbol values Constants i. Numeric constants Characters determining radixes Sample numeric constants Character Radix Numeric constant Value B O Q D H Binary Octal Octal Decimal Hexidecimal 77d 77h Ffffh 0ffffh 88 77 decimal 77 hexadecimal Illegal; doesn’t start with a digit FFFF hexadecimal Interpretation depends on current default radix i. String constants - quoted by single quotes ( ' ' ) or double quotes ( " " ) ii. e.g. 'Hello', 'Mary', 'It''s' (represents "It's"). Symbols. A symbol represents a value, which can be a variable, address label, or an operand to an assembly instruction and directive. i. ii. Symbol names. a. combinations of letters, digits, and special characters like underscore ( _ ), question mark ( ? ), dollar sign ( $ ), etc. b. starting with a letter or a dot ( . ). Symbol types: Symbol type address text_macro alias numerical_expr multiline_macro struc/union table struc/table_member record record_field enum segment group type proctype ASSEMBLY LANGUAGE Description An address. Data subtypes are UNKNOWN, BYTE, WORD, DWORD, PWORD or FWORD, QWORD, TBYTE, and an address of a named structure or table. Code subtypes are SHORT, NEAR, and FAR A text string An equivalent symbol The value of a numerical expression Multiple text lines with dummy arguments A structure or union data type A table data type A structure or table member A record data type A record field An enumerated data type A segment A group A named type A procedure description type page 3 CH/S7CS/Aug., 2002 iii. Address subtypes: Type expression UNKNOWN BYTE WORD DWORD PWORD or FWORD QWORD TBYTE SHORT NEAR FAR PROC DATAPTR CODEPTR struc / union_name table_name record_name enum_name type_name TYPE expression proctype_name Meaning Unknown or undetermined address subtype. Address describes a byte. Address describes a word. Address describes a 4-byte quantity. Address describes a 6-byte quantity. Address describes an 8-byte quantity. Address describes a 10-byte quantity. Address describes a short label/procedure address. Address describes a near label/procedure address. Address describes a far label/procedure address. Address describes either a near or far label/procedure address, depending on the currently selected programming model. Address describes a word, dword, or pword quantity, depending on the currently selected programming model. Address describes either a word, dword, or pword quantity, depending on the currently selected programming model. Address describes an instance of the named structure or union. Address describes an instance of the named table. Address describes an instance of the named record; either a byte, word, or dword quantity. Address describes an instance of the named enumerated data type; either a byte, word, or dword quantity. Address describes an instance of the named type. Address describes an item whose subtype is the address subtype of the expression; Ideal mode only. Address describes procedure of proctype. rem. Several directives let you declare use complex address subtypes, e.g. push WORD PTR [bpt_8] call NEAR PTR _Test Distance syntax Syntax Meaning use a near pointer; can be either 16 or 32 bits, depending on the current model FAR use a far pointer; can be either 32 or 48 bits, depending on the current model SMALL NEAR use a 16-bit pointer; 80386 and 80486 only LARGE NEAR use a 32-bit near pointer; 80386 and 80486 only SMALL FAR use a 32-bit far pointer; 80386 and 80486 only LARGE FAR use a 32-bit far pointer; 80386 and 80486 only Complex address subtypes Syntax Meaning simple_address_subtype the specified address subtype [dist] PTR [complex_address_subtype] a pointer to the specified complex address subtype, the size of which is determined by the current MODEL or by the specified distance, if present NEAR Registers i. Register names are set aside as part of the expression value. e.g. 5+ax+7 gives the value 12 plus the content of the register ax. ii. The common registers are: AX, BX, CX, DX, SI, DI, BP, CS, DS, ES, SS, etc. ASSEMBLY LANGUAGE page 4 CH/S7CS/Aug., 2002 a. Accumulators. AX, BX, CX, DX are used as accumulators, while AL, BL, CL, DL represent the _____________ byte and AH, BH, CH, DH represent the byte of them. b. Segment registers. These are used to hold the segment number. Different segments may be used to keep the programs and data separately. E.g. c. CS - DS - ES - SS - The pointer and index registers. These registers provide offsets that are used in conjunction with the segment registers to access parts of the memory. Index registers: SI - DI - Pointer registers: BP SP - Operators Simple arithmetic operators: Expression Value + expression - expression Expr1 + expr2 Expr1 - expr2 Expr1 * expr2 Expr1 / expr2 Expression. Negative of expression. expr1 plus expr2. expr1 minus expr2. expr1 multiplied by expr2. expr1 divided by expr2 using signed integer division; note that expr2 cannot be 0 or greater than 16 bits in extent. Remainder of expr1 divided by expr2 ; same rules apply as for division. Expr1 MOD expr2 Logical arithmetic operators: Expression Value NOT expression expr1 AND expr2 Expr1 OR expr2 Expr1 XOR expr2 Expression bitwise complemented expr1 bitwise ANDed with expr2 expr1 bitwise ORed with expr2 expr1 bitwise XORed with expr2 Bit shift operators: Expression Value expr1 SHL expr2 expr1 SHR expr2 expr1 shifted left by expr2 bits (shifted right if expr2 is negative). expr1 shifted right by expr2 bits (shifted left if expr2 is negative). Comparison operators: Expression Value expr1 expr1 expr1 expr1 expr1 -1 if -1 if -1 if -1 if -1 if EQ NE GT GE LT expr2 expr2 expr2 expr2 expr2 ASSEMBLY LANGUAGE expr1 expr1 expr1 expr1 expr1 is equal to expr2 ; otherwise, 0. is not equal to expr2 ; otherwise, 0. is greater than expr2 ; otherwise, 0. is greater than or equal to expr2 ; otherwise, 0. is less than expr2 ; otherwise, 0. page 5 CH/S7CS/Aug., 2002 expr1 LE expr2 -1 if expr1 is less than or equal to expr2 ; otherwise, 0. i. The comparison operator returns -1 for true and 0 for not true. ii. Seven other unary operators a. LENGTH - No. of entities represented by a symbol of, say, a table b. SIZE - size information e.g. actual number of bytes allocated to the data variable. c. WIDTH - width in bits of a field in record d. HIGH and LOW - Use the HIGH and LOW operators on an expression to return its high and low byte values. e.g. magic equ 1234h mov cl, HIGH magic mov cl, LOW magic e. ; cl = 12h ; cl = 34h SMALL and LARGE - flags the expression as representing a 16-bit value (SMALL) on a 32-bit value (LARGE). E.g. JMP SMALL [LARGE DWORD PTR ABC] indicates that a large 32-bit address describes the memory variable ABC, but its contents are interpreted as a 16-bit segment and 16-bit offset. ** Overriding the segment part of an address expression. Address expressions have values consisting of a segment and an offset. The segment used is implicitly assigned to be the value in DS, but you can override it by explicitly specifying a new one. The following is the syntax: expr1:expr2 where expr2 - the offset, expr1 - the segment or group value e.g. VarPtr dd dgroup : memvar mov cl, es:[si+4] The SEG directive returns the segment or group value where the OFFSET returns the offset of the address expression. Describing the content of an address using square brackets ([ ]) as follows: mov ax, bx mov ax, [bx] mov ax, 5[bx] mov ax, 5(XYZ) ; move BX into AX ; move contents of address stored in BX into AX ; move contents of address stored in BX + 5 into AX ; move contents of address XYZ + 5 into AX Instructions MOV It copies the second operand to the first operand. Its operands can be a register or memory address. Examples: mov bx, [bp+6] ;move the content of address of 6 plus\ the content of bp to register bx mov dx, OFFSET TimePrompt ;point to the time prompt in program\ in p.1 ASSEMBLY LANGUAGE page 6 CH/S7CS/Aug., 2002 ADD / SUB / MUL / DIV / IMUL ADD performs an integer addition of the two operands (DEST and SRC). The result of the addition is assigned to the first operand (DEST), and the flags are set accordingly. SUB subtracts the second operand (SRC) from the first operand (DEST). The result of the subtraction is assigned to the first operand (DEST), and the flags are set accordingly. MUL(IMUL) performs an unsigned (signed) multiplication. Its actions depend on the size of its operand, as follows: i. A byte operand is multiplied by AL; the result is left in AX. ii. A word operand is multiplied by AX; the result is left in DX and AX. DX contains the high-order 16 bits of the product. iii. A double operand is multiplied by EAX and the result is left in EDX and EAX. EDX contains the highorder 32 bits of the product. DIV(IDIV) performs an unsigned (signed) division. The dividend is implicit; only the divisor is given as an operand. The type of the divisor determines which registers to use as follows: Size Dividend Divisor Quotient Remainder Byte word Dword AX DX:AX EDX:EAX r/m8 r/m16 r/m32 AL AX EAX AH DX EDX(386 only) Examples: add ax, es:[bx] ;add the value in ax to the value stored in the\ extra segment with offset stored in bx. div bl ;divide ax by bl with quotient in al, remainder in ah mul bx ;multiply ax by bx, high order result in dx\ low order result in ax INC/DEC INC adds 1 to the operand. It DOES NOT change the carry flag. DEC subtract 1 from the operand. It also DOES NOT change the carry flag. Example: inc dec [RunningTotal] di ;RunningTotal++ ;point back the destination pointer CMP CMP subtracts the second operand form the first one but, unlike the SUB instruction, DOES NOT store the result; only the flags are changed. CMP is typically used in conjunction with conditional jumps and the SETcc instruction. Example: cmp je al,'Y' IsAfternoon ;compare with 'Y' ;jump to the label IsAfternoon if equal The result of an arithmetic instruction or CMP may set different values of 4 bits, N, Z, V and C in the status register on follows i. If the result is negative, N= 1. ii. If the result is zero, Z = 1. iii. If overflow occurred, V = 1. ASSEMBLY LANGUAGE page 7 CH/S7CS/Aug., 2002 iv. If carry occurred, C = 1. JMP JMP transfers control to a different point in the instruction stream without recording return information. Example jmp FarLabel Jcc cc is a condition type. The conditional commands are list as follows: Instruction JA rel8 JAE rel8 JB rel8 JBE rel8 JC rel8 JCXZ rel8 JECXZ rel8 JE rel8 JZ rel8 JG rel8 JGE rel8 JL rel8 JLE rel8 JNA rel8 JNAE rel8 JNB rel8 JNBE rel8 JNC rel8 JNE rel8 JNG rel8 JNGE rel8 JNL rel8 JNLE rel8 JNO rel8 JNP rel8 JNS rel8 JNZ rel8 JO rel8 JP rel8 JPE rel8 JPO rel8 JS rel8 JZ rel8 JA rel16/32 JAE rel16/32 JB rel16/32 JBE rel16/32 JC rel16/32 JE rel16/32 JZ rel16/32 JG rel16/32 JGE rel16/32 JL rel16/32 JLE rel16/32 JNA rel16/32 JNAE rel16/32 JNB rel16/32 JNBE rel16/32 JNC rel16/32 JNE rel16/32 JNG rel16/32 JNGE rel16/32 JNL rel16/32 JNLE rel16/32 JNO rel16/32 JNP rel16/32 JNS rel16/32 ASSEMBLY LANGUAGE Description Jump short if above (CF=0 and ZF=0) Jump short if above or equal (CF=0) Jump short if below (CF=1) Jump short if below or equal (CF=1 or ZF=1) Jump short if carry (CF=1) Jump short if CX register is 0 Jump short if ECX register is 0 Jump short if equal (ZF=1) Jump short if 0 (ZF=1) Jump short if greater (ZF=0 and SF=OF) Jump short if greater or equal (SF=OF) Jump short if less (SF<>OF) Jump short if less or equal (SF<>OF and ZF=0) Jump short if not above (CF=1 or ZF=1) Jump short if not above or equal (CF=1) Jump short if not below (CF=1) Jump short if not below or equal (CF=1 and ZF=0) Jump short if not carry (CF=0) Jump short if not equal (ZF=0) Jump short if not greater (ZF=1 or SF<>OF) Jump short if not greater or equal (SF<>OF) Jump short if not less (SF=OF) Jump short if not less or equal (ZF=0 and SF=OF) Jump short if not overflow (OF=0) Jump short if not parity (PF=0) Jump short if not sign (SF=0) Jump short if not zero (ZF=0) Jump short if overflow (OF=1) Jump short if parity (PF=1) Jump short if parity even (PF=1) Jump short if parity odd (PF=0) Jump short if sign (SF=0) Jump short if zero (ZF=1) Jump near if above (CF=0 and ZF=0) Jump near if above or equal (CF=0) Jump near if below (CF=1) Jump near if below or equal (CF=1 or ZF=1) Jump near if carry (CF=1) Jump near if equal (ZF=1) Jump near if 0 (ZF=1) Jump near if greater (ZF=0 and SF<>OF) Jump near if greater or equal (SF=OF) Jump near if less (SF<>OF) Jump near if less or equal (ZF=1 and SF<>OF) Jump near if not above (CF=1 or ZF=1) Jump near if not above or equal (CF=1) Jump near if not below (CF=0) Jump near if not below or equal (CF=0 and ZF=0) Jump near if not carry (CF=0) Jump near if not equal (ZF=0) Jump near if not greater (ZF=1 or SF<>OF) Jump near if not greater or equal (SF<>OF) Jump near if not less (SF=OF) Jump near if not less or equal (ZF=0 and SF=OF) Jump near if not overflow (OF=0) Jump near if not parity (PF=0) Jump near if not sign (SF=0) page 8 CH/S7CS/Aug., 2002 JNZ rel16/32 JO rel16/32 JP rel16/32 JPE rel16/32 JPO rel16/32 JS rel16/32 JZ rel16/32 Jump near if not zero (ZF=0) Jump near if overflow (OF=1) Jump near if parity (PF=1) Jump near if parity even (PF=1) Jump near if parity odd (PF=0) Jump near if sign (SF=1) Jump near if zero (ZF=1) Conditional jumps (except JCXZ/JECSZ) test the flags which have been set by a previous instruction. The conditions for each mnemonic are given in parentheses after each description above. (The terms “less” and “greater” are used for comparisons of signed integers; “above” and “below” are used for unsigned integers.) If the given condition is true, a jump is made to the location provided as the operand. When the target for the conditional jump is in a different segment, use the opposite case of the jump instruction and then access the target with an unconditional far jump to the other segment. Example: JZ FARLABEL JNZ BEYOND JMP FARLABEL BEYOND ;illegal if the label FARLABEL is in a \ ;different segment ; ; ; legal CALL/RET CALL causes the procedure named in the operand to be executed. When the procedure is completed (a return instruction is executed within the procedure), execution continues at the instruction that follows the CALL instruction. Example: CALL FAR PTR test2 RET transfers control to a return address located on the stack. The address is usually placed on the stack by a CALL instruction, and the return is made to the instruction that follows the CALL. Example: test1 PROC NEAR mov ax,10 call FAR PTR test2 RET test1 ENDP INT INT generates via software a call to an interrupt handler. The immediate operand, from 0 to 255 (ffh), gives the index number into the interrupt descriptor table (IDT) of the interrupt routine to be called. There are several useful interrupt service routines available in DOS. To select different DOS functions, a number should be first placed in ah and then call the interrupt handler 21h. Here are some of the DOS function value in ah 1 2 9 4ch function get a character from keyboard into al display a character to screen from dl display a character to screen from the memory locations pointed by dx DOS terminate program ASSEMBLY LANGUAGE page 9 CH/S7CS/Aug., 2002 Example: mov int mov mov int mov int ah, 21h ah, dl, 21h ah, 21h 1 ;get next key pressed 2 al ;move character read from AL to DL ;display the character 4ch ;DOS terminate function Note that DOS can only read in character or character string, so if you want to accept an integer from keyboard, you have to write a program to convert an input "integer” into numeric value in storage, binary form. PUSH/POP PUSH decrements the stack pointer by 2 if the operand-size attribute of the instruction is 16 bits; otherwise, it decrements the stack pointer by 4, if that is 32 bits. Then it places the operand on the new top of stack, which is pointer to by the stack pointer. POP copies the content of the top element into operand; and then increments the stack pointer by 2 if the operand-size attribute of the instruction is 16 bits; otherwise, it increments the stack pointer by 4, if that is 32 bits. Example: mov push pop ax, constant ax bx LEA LEA calculates the effective address (offset part) and stores it in the specified register. Example: lea mov inc bx,[LocalArray] [bx],al bx ;point to local array ;fill next byte Directives There are operations that DO NOT form part of the actual program. They are not assembly language instructions, but instructions to assembler. e.g. END, EQU Allocating Data Data allocation directives are used for allocating bytes in a segment for filling those bytes with initial data, and for defining data variables. The general syntax of all data allocation directives: [name] directive dup_expr [, dup_expr] while dup_expr can be one of the followings: i. ? ii. value iii. count_expression DUP (dup_expr[, dup_expr...]) ASSEMBLY LANGUAGE page 10 CH/S7CS/Aug., 2002 e.g. Different sizes of simple data are shown in the following table ABC DW 2 DUP (3 DUP (1, 3), 5) ; same as DW 1,3,1,3,1,3,5,1,3,1,3,1,3,5 Directive Meaning DB DW DD DQ DF DP DT Define byte-size data. Define word-size data. Define doubleword-size data. Define quadword-size data. Define 48-bit 80386 far-pointer size (6-byte) data. Define 48-bit 80386 far-pointer size (6-byte) data. Define tenbyte (10-byte) size data. Data is always stored in memory low value before high value. Legal examples: A B C D E DB "xyz" DD 1.28+5 DD 01234567 DT ? DW 10 DUP (?) ; ; ; ; ; string A (each character byte) real number B real number C uninitialized D 10-element uninitialized array E DD ; too short in width Illegal examples: D 1234567h Declaring Procedures PROC directive - for declaring procedure Syntax: name PROC [[language modifier] language][distance] [ARG argument_list] [RETURN item_list] [LOCAL argument_list] [USES item_list] : : [name] ENDP There are 2 types of procedure: NEAR and FAR. i. NEAR procedures are called with a near call, and contain a near return; you must call them only from within the same segment. ii. FAR procedures are called with a far call and contain far returns. You can call FAR procedures outside the segment in which they are defined. Example. : MODEL TINY ;default distance near : ;test1 is a far procedure test1 PROC FAR ;body of procedure RET ;this will be a far return ENDP ;test2 is by default a near procedure test2 PROC ;body of procedure RET ;this will be a near return ENDP : ASSEMBLY LANGUAGE page 11 CH/S7CS/Aug., 2002 Using the CALL directive to invoke the above two procedures, : CALL test1 CALL test2 ; this is a far call ; this is a near call : Defining arguments and local variables ARG directive i. The directive specifies the stack frame arguments passed to procedure. internally as positive offsets from the BP or EBP registers. ii. Syntax: Arguments are defined ARG argument [,argument] ... [=symbol] [RETURNS argument [,argument]] An individual argument has the following syntax: argname [[count1_expression]] [: complex_type [:count2_expression]] e.g. ARG tmp:DWORD:4 defines an argument called tmp, consisting of 4 double words. LOCAL directives i. The directives define the local variables for the procedure. They specify the stack frame variable local to procedure. Local variables are defined internally as negative offset from the BP or EBP registers. ii. If you end the argument or local variable list with an equal sign (=) and a symbol, Turbo Assembler will equate the symbol to the total size of the argument or local variable block in bytes. e.g. : : func1 PROC NEAR ARG a:WORD, b:DWORD:4, c:BYTE=d LOCAL x:DWORD, y:WORD:2=z : defines a as [bp+4], b as [bp+6], c as [bp+22], d as 20; x is [bp-2], y is [bp-6] and z is 8. (default count expression for BYTE type is 2, and that for other types is 1) Macros Macros let you give a symbolic name to represent text string or a block of code that will be used frequently through out your program. Every time the macro is encountered within the source code all the necessary machine codes instructions are generated. Advantage. Only one instruction has to be changed if a complete new routine is required when the assembler is used. Disadvantage. It can create very long object programs if not used with care. ASSEMBLY LANGUAGE page 12 CH/S7CS/Aug., 2002 Text macros The EQU directive to define simple text macros. Here is the syntax: name EQU text_string where text_string should be enclosed with angle brackets (< >) DoneMsg EQU <'Returning to the OS'> A EQU DoneMsg ; Wrong! A EQU <DoneMsg> ; Right. Goodbye DB DoneMsg ; Right. e.g. Note that DoneMsg is NOT a variable. String Macro manipulation directive i. CATSTR. This directive defines a new text macro by concatenating strings together. ii. SUBSTR. This directive defines a new text macro to be substring of a string. iii. INSTR. This directive returns the position of one string inside another string. iv. SIZESTR. This directive returns the length of text macro (the number of characters in the string. Examples: VERSION T300 IDEAL ABC ABC2 ABC ABC3 ABCLEN ABC3LEN COMMA1 COMMA2 ABC4 ABC5 ABC6 ABC7 ABC8 EQU EQU EQU CATSTR SIZESTR SIZESTR INSTR INSTR SUBSTR SUBSTR EQU EQU EQU <abc> <ABC> <def> ABC2,<,>,ABC,<,>,ABC2 ABC ABC3 ABC3,<,> COMMA1+1,ABC3,<,> ABC3,5 ABC3,5,3 3+2+1 %3+2+1 %COMMA1 ;ABC = "abc" ;ABC2 = "abc" ;ABC = "def" (redefined) ;ABC3 = "abc,def,abc" ;ABCLEN = 3 ;ABC3LEN = 11 ;COMMA1 = 4 ;COMMA2 = 8 ;ABC4 = "def,abc" ;ABC5 = "def" ;ABC6 = 6 (numeric equate) ;ABC7 = "6" (text macro) ;ABC8 = "4" Multiline macros The multiline macro facility lets you define a body of instructions, directives, or other macros that will be included in your source code whenever the macro is invoked. Turbo Assembler lets you replace symbols within the macro body with text specified at the time a macro is invoked, i.e. dummy argument. The character & separates a dummy argument name from surrounding text. General syntax: name MACRO parameter_list macro_body ENDM e.g. PUSHALL MACRO PUSH AX BX CX DX PUSH DS SI PUSH ES DI ENDM parameter_list in the syntax is a list of dummy argument symbols for the macro. e.g. ADDUP MACRO dest,\ ;dest is 1st dummy argument s1, s2 ;s1, s2 are 2nd and 3rd dummy arguments mov dest, s1 add dest, s2 ENDM ASSEMBLY LANGUAGE page 13 CH/S7CS/Aug., 2002 Syntax for invoking macros macro_name [argument [[,]argument]...] The character & separates a dummy argument name from surrounding text. e.g. macro mac1 foo sym&foo: DB 'It is &foo time' endm If foo is assigned the text string party when this macro is invoked, the actual text included in the macro will be: Symparty: DB 'It is party time' Special character for macro expansion <> - literal string brackets ! - single character % - expression evaluation character Examples: DEFSYM MACRO NUM TMP_&NUM ENDM TNAME EQU <JUNK> DEFSYM %9 DEFSYM !! DEFSYM !> ; returning TMP_9 ; returning TMP_! ; returning TMP_> Conditional Directives: IF...ELSE...ENDIF General syntax Ifxxx [condition] true_conditional_body [ELSE false_conditional_body] ENDIF If the condition is true, the block of assembly code in true_condition_body is assembled into output object file. If the condition evaluates to false, Turbo Assembler skips over true_condition_body and does not include it in the object file. If there is an ELSE directive, the false_conditional_body is assembled into the object file if the condition is false; it is ignored if the conditioned is true. All conditionals are terminated with an ENDIF directive. e.g. : IFDEF test ;test code 1 IF color ;color code ELSE ;mono code ENDIF ;test code 2 ELSE ;non-test code ENDIF : ASSEMBLY LANGUAGE ; ; ; ; ; ; ; ; ; ; if test defined if color <> 0 if color = 0 if test defined if test not defined page 14 CH/S7CS/Aug., 2002 The Assembler Main features of an assembler In general, it enables users to i. use symbolic addressing. ii. perform arithmetic. iii. use different bases. iv. be alerted to any errors during the assembly process. v. tell the loader program where parts of the program or data should be in the computer’s memory. vi. produce a listing of the source code or object code, together with error messages. vii. use macros. Types of assembler (not mutually exclusive) i. Resident assembler. It will only translate the mnemonics into the specific machine code for the processor to resident in the particular machine. ii. Cross assembler. It is able to produce the object code that run on a different machine. iii. Two-pass assembler. When source code is to be assembled, this type of assembler will a. FIRST go through the source code once producing the object code, but will ignore any forward references such as jumps; AND build the symbol table. (first pass) b. THEN go through and resolve all the jump address to produce the final object code. (second pass) iv. One-pass assembler. It only needs to go through the assembly process once. v. Macro assembler. This type of assembler support macros. Loader It is a program that takes the object code and loads it into the computer’s main memory in the correct place. Types of loader i. Bootstrap loader. It loads itself by using the instruction at the beginning to load the rest of itself. ii. Linking loader. libraries) iii. Relocating loader. which can’t. ASSEMBLY LANGUAGE It is able to link together programs that have been assembled separately. (e.g. It loads that object code anywhere in memory as opposed to an absolute loader page 15 CH/S7CS/Aug., 2002 Sample Programs 1. DECIBIN - Converting decimal number to binary number. (a listing file by tasm/l) Turbo Assembler test2.ASM 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 Version 2.02 09/13/95 09:21:19 Page 1 ;DECIBIN--Program to get decimal digits from ;keyboard and convert them to binary number in BX 0000 0000 0000 0000 0000 0003 0003 0005 0007 0009 000B 000D 000F BB 0000 .MODEL SMALL .STACK 100h .CODE start: mov B4 CD 2C 7C 3C 7F 98 ;Get digit from keyboard, convert to binary newchar: mov ah,1 ;keyboard input int 21h ;call DOS sub al, 30h ;ASCII to binary jl exit ;jump if < 0 cmp al,9d ;is it > 9d ? jg exit ;yes, not dec digit cbw ;byte in AL to word in AX 01 21 30 10 09 0C bx, 0 ;clear BX for number 0010 0011 0014 0016 93 B9 000A F7 E1 93 ;(digit is now in AX) ;Multiply number in BX by 10 decimal xchg ax,bx ;trade digit & number mov cx,10d ;put 10 dec in CX mul cx ;number times 10 xchg ax, bx ;trade number & digit 0017 0019 03 D8 EB E8 ;Add digit in AX to number in BX add bx,ax ;add digit to number jmp newchar ;get next digit 001B 001B 001D 001F B4 4C B0 00 CD 21 exit: Turbo Assembler Symbol Table mov mov int ah,4ch al,0 21h ;DOS: terminate program ;return code will be 0 end start Version 2.02 09/13/95 09:21:19 Symbol Name Type ??DATE ??FILENAME ??TIME ??VERSION @CODE @CODESIZE @CPU @CURSEG @DATA @DATASIZE @FILENAME @MODEL @WORDSIZE EXIT NEWCHAR START Text "09/13/95" Text "test2 " Text "09:21:19" Number 0202 Text _TEXT Text 0 Text 0101H Text _TEXT Text DGROUP Text 0 Text TEST2 Text 2 Text 2 Near _TEXT:001B Near _TEXT:0003 Near _TEXT:0000 ASSEMBLY LANGUAGE Page 2 Value page 16 CH/S7CS/Aug., 2002 2. PIANO - run speaker number keys play notes of the scale ;PIANO -- Use Timer2 to run speaker number keys play notes of the scale .MODEL SMALL .STACK 100h .DATA dw 262d ;C dw 294d ;D dw 330d ;E dw 347d ;F dw 392d ;G dw 440d ;A dw 494d ;B dw 524d ;C portB equ 61h ;I/O Port B keybd2 equ 7h ;keybd input. no echo doscall equ 21h ;DOS interrupt number cont_c equ 03h ;control-C ASCII code .CODE start: mov ax, @data mov ds, ax ;set DS to point to the data segment ;read keyboard to get digit from 0 to 7 read_key: mov ah, keybd2 ;keybd funct. no echo int doscall ;call DOS cmp al, cont_c ;is it control-C ? jz exit ;yes, so exit sub al, 31h ;change ASCII to digit and al, 00000111b ;mask off upper 5 bits shl ax, 1 ;* by 2 (2 byte/word) cbw ;byte --> word in AX mov bx, ax ;put in BX (for table) mov ax, 0 ;numerator (low word) mov dx, 12h ;(high word) div [WORD PTR bx] ;divisor from table mov bx, ax ;save quotient in BX ;set 1/pitch into time, then turn on tone mov al, 10110110b ;put magic number out 43h, al ; into timer2 mov ax, bx ;1/pitch into AX out 42h, al ;LSB into timer2 mov al, ah ;MSB to AL. then out 42h, al ; into timer2 in al, portB ;read port B into AL or al, 3 ;turn on bits 0 and 1 out portB, al ;to turn on speaker ;sound note for a while, then turn it off mov cx, 0fffh ;set up for delay wait1: loop wait1 ;delay in al, portB ;read port B into AL and al, 11111100b ;mask lower 2 bits out portB, al ;to turn off speaker jmp read_key ;go get another digit ;control-C typed, so exit exit: mov ah, 4ch ;DOS: terminate program mov al, 0 ;return code will be 0 int 21h ;terminate the program end start ASSEMBLY LANGUAGE page 17