content of information processing - kwun tong · web viewopcode of machine codes. are...

24
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 languages in which each instruction corresponds to or resembles . 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 DB 'Is it after 12 noon (Y/N)?$' GoodMorningMessage DB 13,10,'Good morning, World!',13,10,'$' GoodAfternoonMessage DB 13,10,'Good afternoon, World!',13,10,'$' DefaultMessage DB 13,10,'Greetings, World!',10,13,'$' ASSEMBLY LANGUAGE page 1

Upload: ngoxuyen

Post on 20-Mar-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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 languages in which each instruction corresponds to or resembles .

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 DB 'Is it after 12 noon (Y/N)?$'GoodMorningMessage DB 13,10,'Good morning, World!',13,10,'$'GoodAfternoonMessage DB 13,10,'Good afternoon, World!',13,10,'$'DefaultMessage DB 13,10,'Greetings, World!',10,13,'$'

.CODEstart: mov ax,@data mov ds,ax ;set DS to point to the data mov dx,OFFSET TimePrompt ;point to the time prompt mov ah,9 ;DOS: print string int 21h ;display the time prompt mov ah,1 ;DOS: get character int 21h ;get a single-character response or al,20h ;force character to lower case cmp al,'y' ;typed Y for afternoon? je IsAfternoon cmp al,'n' ;typed N for morning? je IsMorning

mov dx,OFFSET DefaultMessage ;default greeting

ASSEMBLY LANGUAGE page 1

Page 2: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

jmp DisplayGreeting

IsAfternoon: mov dx,OFFSET GoodAfternoonMessage ;afternoon greeting jmp DisplayGreeting

IsMorning: mov dx,OFFSET GoodMorningMessage ;before noon greeting

DisplayGreeting: mov ah,9 ;DOS: print string int 21h ;display the appropriate greeting mov ah,4ch ;DOS: terminate program mov al,0 ;return code will be 0 int 21h ;terminate the program

END start

Commenting the program

i. Comments at end of the line, starting with a semicolon (;).

e.g. mov [bx], al ;store the modified character.

ii. 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

Page 3: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

Expressions and Symbol values

Constants

i. Numeric constants

Characters determining radixes Sample numeric constants

Character Radix Numeric constant Value

B Binary 77d 77 decimalO Octal 77h 77 hexadecimalQ Octal Ffffh Illegal; doesn’t start with a

digitD Decimal 0ffffh FFFF hexadecimalH Hexidecimal 88 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. 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 ( . ).

ii. Symbol types:

Symbol type Descriptionaddress 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

text_macro A text stringalias An equivalent symbolnumerical_expr The value of a numerical expressionmultiline_macro Multiple text lines with dummy argumentsstruc/union A structure or union data typetable A table data typestruc/table_member A structure or table memberrecord A record data typerecord_field A record fieldenum An enumerated data typesegment A segmentgroup A grouptype A named typeproctype A procedure description type

ASSEMBLY LANGUAGE page 3

Page 4: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

iii. Address subtypes:

Type expression MeaningUNKNOWN Unknown or undetermined address subtype.BYTE Address describes a byte.WORD Address describes a word.DWORD Address describes a 4-byte quantity.PWORD or FWORD Address describes a 6-byte quantity.QWORD Address describes an 8-byte quantity.TBYTE Address describes a 10-byte quantity.SHORT Address describes a short label/procedure address.NEAR Address describes a near label/procedure address.FAR Address describes a far label/procedure address.PROC Address describes either a near or far label/procedure address,

depending on the currently selected programming model.DATAPTR Address describes a word, dword, or pword quantity, depending on

the currently selected programming model.CODEPTR Address describes either a word, dword, or pword quantity,

depending on the currently selected programming model.struc / union_name Address describes an instance of the named structure or union.table_name Address describes an instance of the named table.record_name Address describes an instance of the named record; either a byte,

word, or dword quantity.enum_name Address describes an instance of the named enumerated data type;

either a byte, word, or dword quantity.type_name Address describes an instance of the named type.TYPE expression Address describes an item whose subtype is the address subtype of

the expression; Ideal mode only.proctype_name 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 syntaxSyntax Meaning

NEAR 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 onlyLARGE NEAR use a 32-bit near pointer; 80386 and 80486 onlySMALL FAR use a 32-bit far pointer; 80386 and 80486 onlyLARGE FAR use a 32-bit far pointer; 80386 and 80486 only Complex address subtypes

Syntax Meaningsimple_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

Registersi. 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

Page 5: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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.

CS - DS -

ES - SS -

c. 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.- expression Negative of expression.Expr1 + expr2 expr1 plus expr2.Expr1 - expr2 expr1 minus expr2.Expr1 * expr2 expr1 multiplied by expr2.Expr1 / expr2 expr1 divided by expr2 using signed integer

division; note that expr2 cannot be 0 or greater than 16 bits in extent.

Expr1 MOD expr2 Remainder of expr1 divided by expr2 ; same rules apply as for division.

Logical arithmetic operators:

Expression ValueNOT expression Expression bitwise complementedexpr1 AND expr2 expr1 bitwise ANDed with expr2Expr1 OR expr2 expr1 bitwise ORed with expr2Expr1 XOR expr2 expr1 bitwise XORed with expr2

Bit shift operators:

Expression Value

expr1 SHL expr2 expr1 shifted left by expr2 bits (shifted right if expr2 is negative).expr1 SHR expr2 expr1 shifted right by expr2 bits (shifted left if expr2 is negative).

Comparison operators:

Expression Value

expr1 EQ expr2 -1 if expr1 is equal to expr2 ; otherwise, 0.expr1 NE expr2 -1 if expr1 is not equal to expr2 ; otherwise, 0.expr1 GT expr2 -1 if expr1 is greater than expr2 ; otherwise, 0.expr1 GE expr2 -1 if expr1 is greater than or equal to expr2 ; otherwise, 0.expr1 LT expr2 -1 if expr1 is less than expr2 ; otherwise, 0.expr1 LE expr2 -1 if expr1 is less than or equal to expr2 ; otherwise, 0.

ASSEMBLY LANGUAGE page 5

Page 6: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

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 1234hmov cl, HIGH magic ; cl = 12hmov cl, LOW magic ; cl = 34h

e. 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 : memvarmov 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 ; move BX into AXmov ax, [bx] ; move contents of address stored in BX into AXmov ax, 5[bx] ; move contents of address stored in BX + 5 into AXmov ax, 5(XYZ) ; 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 bxmov dx, OFFSET TimePrompt ;point to the time prompt in program\in p.1

ASSEMBLY LANGUAGE page 6

Page 7: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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 high-order 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 RemainderByte AX r/m8 AL AHword DX:AX r/m16 AX DX

Dword EDX:EAX r/m32 EAX 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 ahmul 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 [RunningTotal] ;RunningTotal++dec di ;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 al,'Y' ;compare with 'Y' je IsAfternoon ;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

Page 8: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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.

Examplejmp FarLabel

Jcc

cc is a condition type. The conditional commands are list as follows:

Instruction DescriptionJA rel8 Jump short if above (CF=0 and ZF=0)JAE rel8 Jump short if above or equal (CF=0)JB rel8 Jump short if below (CF=1)JBE rel8 Jump short if below or equal (CF=1 or ZF=1)JC rel8 Jump short if carry (CF=1)JCXZ rel8 Jump short if CX register is 0JECXZ rel8 Jump short if ECX register is 0JE rel8 Jump short if equal (ZF=1)JZ rel8 Jump short if 0 (ZF=1)JG rel8 Jump short if greater (ZF=0 and SF=OF)JGE rel8 Jump short if greater or equal (SF=OF)JL rel8 Jump short if less (SF<>OF)JLE rel8 Jump short if less or equal (SF<>OF and ZF=0)JNA rel8 Jump short if not above (CF=1 or ZF=1)JNAE rel8 Jump short if not above or equal (CF=1)JNB rel8 Jump short if not below (CF=1)JNBE rel8 Jump short if not below or equal (CF=1 and ZF=0)JNC rel8 Jump short if not carry (CF=0)JNE rel8 Jump short if not equal (ZF=0)JNG rel8 Jump short if not greater (ZF=1 or SF<>OF)JNGE rel8 Jump short if not greater or equal (SF<>OF)JNL rel8 Jump short if not less (SF=OF)JNLE rel8 Jump short if not less or equal (ZF=0 and SF=OF)JNO rel8 Jump short if not overflow (OF=0) JNP rel8 Jump short if not parity (PF=0)JNS rel8 Jump short if not sign (SF=0)JNZ rel8 Jump short if not zero (ZF=0)JO rel8 Jump short if overflow (OF=1)JP rel8 Jump short if parity (PF=1)JPE rel8 Jump short if parity even (PF=1)JPO rel8 Jump short if parity odd (PF=0)JS rel8 Jump short if sign (SF=0)JZ rel8 Jump short if zero (ZF=1)JA rel16/32 Jump near if above (CF=0 and ZF=0)JAE rel16/32 Jump near if above or equal (CF=0)JB rel16/32 Jump near if below (CF=1)JBE rel16/32 Jump near if below or equal (CF=1 or ZF=1)JC rel16/32 Jump near if carry (CF=1)JE rel16/32 Jump near if equal (ZF=1)JZ rel16/32 Jump near if 0 (ZF=1)JG rel16/32 Jump near if greater (ZF=0 and SF<>OF)JGE rel16/32 Jump near if greater or equal (SF=OF)JL rel16/32 Jump near if less (SF<>OF)JLE rel16/32 Jump near if less or equal (ZF=1 and SF<>OF)JNA rel16/32 Jump near if not above (CF=1 or ZF=1)JNAE rel16/32 Jump near if not above or equal (CF=1)JNB rel16/32 Jump near if not below (CF=0)JNBE rel16/32 Jump near if not below or equal (CF=0 and ZF=0)JNC rel16/32 Jump near if not carry (CF=0)JNE rel16/32 Jump near if not equal (ZF=0)JNG rel16/32 Jump near if not greater (ZF=1 or SF<>OF)JNGE rel16/32 Jump near if not greater or equal (SF<>OF)JNL rel16/32 Jump near if not less (SF=OF)JNLE rel16/32 Jump near if not less or equal (ZF=0 and SF=OF)JNO rel16/32 Jump near if not overflow (OF=0)JNP rel16/32 Jump near if not parity (PF=0)JNS rel16/32 Jump near if not sign (SF=0)

ASSEMBLY LANGUAGE page 8

Page 9: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

JNZ rel16/32 Jump near if not zero (ZF=0)JO rel16/32 Jump near if overflow (OF=1)JP rel16/32 Jump near if parity (PF=1)JPE rel16/32 Jump near if parity even (PF=1)JPO rel16/32 Jump near if parity odd (PF=0)JS rel16/32 Jump near if sign (SF=1)JZ rel16/32 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 ;illegal if the label FARLABEL is in a \

;different segment

JNZ BEYOND ;JMP FARLABEL ; legalBEYOND ;

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,10call FAR PTR test2RET

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 function1 get a character from keyboard into al2 display a character to screen from dl9 display a character to screen from the memory locations pointed by

dx4ch DOS terminate program

ASSEMBLY LANGUAGE page 9

Page 10: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

Example:mov ah, 1int 21h ;get next key pressedmov ah, 2mov dl, al ;move character read from AL to DLint 21h ;display the charactermov ah, 4chint 21h ;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 ax, constantpush axpop bx

LEA

LEA calculates the effective address (offset part) and stores it in the specified register.

Example:lea bx,[LocalArray] ;point to local arraymov [bx],al ;fill next byteinc bx

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

Page 11: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

e.g. 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

Different sizes of simple data are shown in the following table

Directive Meaning

DB Define byte-size data.DW Define word-size data.DD Define doubleword-size data.DQ Define quadword-size data.DF Define 48-bit 80386 far-pointer size (6-byte) data.DP Define 48-bit 80386 far-pointer size (6-byte) data.DT Define tenbyte (10-byte) size data.

Data is always stored in memory low value before high value.

Legal examples:A DB "xyz" ; string A (each character byte)B DD 1.28+5 ; real number BC DD 01234567 ; real number CD DT ? ; uninitialized DE DW 10 DUP (?) ; 10-element uninitialized array E

Illegal examples:D DD 1234567h ; too short in width

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 proceduretest1 PROC FAR

;body of procedureRET ;this will be a far return

ENDP;test2 is by default a near proceduretest2 PROC

;body of procedureRET ;this will be a near return

ENDP:

ASSEMBLY LANGUAGE page 11

Page 12: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

Using the CALL directive to invoke the above two procedures,:

CALL test1 ; this is a far callCALL test2 ; this is a near call

:

Defining arguments and local variables

ARG directive

i. The directive specifies the stack frame arguments passed to procedure. Arguments are defined internally as positive offsets from the BP or EBP registers.

ii. Syntax: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 NEARARG a:WORD, b:DWORD:4, c:BYTE=dLOCAL 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

Page 13: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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 (< >)

e.g. DoneMsg EQU <'Returning to the OS'>A EQU DoneMsg ; Wrong!A EQU <DoneMsg> ; Right.Goodbye DB DoneMsg ; Right.

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 T300IDEALABC EQU <abc> ;ABC = "abc"ABC2 EQU <ABC> ;ABC2 = "abc"ABC EQU <def> ;ABC = "def" (redefined)ABC3 CATSTR ABC2,<,>,ABC,<,>,ABC2 ;ABC3 = "abc,def,abc"ABCLEN SIZESTR ABC ;ABCLEN = 3ABC3LEN SIZESTR ABC3 ;ABC3LEN = 11COMMA1 INSTR ABC3,<,> ;COMMA1 = 4COMMA2 INSTR COMMA1+1,ABC3,<,> ;COMMA2 = 8ABC4 SUBSTR ABC3,5 ;ABC4 = "def,abc"ABC5 SUBSTR ABC3,5,3 ;ABC5 = "def"ABC6 EQU 3+2+1 ;ABC6 = 6 (numeric equate)ABC7 EQU %3+2+1 ;ABC7 = "6" (text macro)ABC8 EQU %COMMA1 ;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_listmacro_bodyENDM

e.g.PUSHALL MACRO

PUSH AX BX CX DXPUSH DS SIPUSH 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 argumentsmov dest, s1add dest, s2

ASSEMBLY LANGUAGE page 13

Page 14: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

ENDM

Syntax for invoking macrosmacro_name [argument [[,]argument]...]

The character & separates a dummy argument name from surrounding text. e.g.macro mac1 foosym&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 characterExamples:

DEFSYM MACRO NUMTMP_&NUMENDM

TNAME EQU <JUNK>DEFSYM %9 ; returning TMP_9DEFSYM !! ; returning TMP_!DEFSYM !> ; returning TMP_>

Conditional Directives: IF...ELSE...ENDIF General syntax

Ifxxx [condition]true_conditional_body

[ELSEfalse_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 test defined IF color ;

;color code ; if color <> 0 ELSE ;

;mono code ; if color = 0 ENDIF ;

;test code 2 ; if test definedELSE ;

;non-test code ; if test not definedENDIF

:

ASSEMBLY LANGUAGE page 14

Page 15: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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. It is able to link together programs that have been assembled separately. (e.g. libraries)

iii. Relocating loader. It loads that object code anywhere in memory as opposed to an absolute loader which can’t.

ASSEMBLY LANGUAGE page 15

Page 16: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

CH/S7CS/Aug., 2002

Sample Programs

1. DECIBIN - Converting decimal number to binary number. (a listing file by tasm/l)

Turbo Assembler Version 2.02 09/13/95 09:21:19 Page 1test2.ASM

1 ;DECIBIN--Program to get decimal digits from 2 ;keyboard and convert them to binary number in BX 3 4 0000 .MODEL SMALL 5 0000 .STACK 100h 6 0000 .CODE 7 0000 start: 8 0000 BB 0000 mov bx, 0 ;clear BX for number 9 10 ;Get digit from keyboard, convert to binary 11 0003 newchar: 12 0003 B4 01 mov ah,1 ;keyboard input 13 0005 CD 21 int 21h ;call DOS 14 0007 2C 30 sub al, 30h ;ASCII to binary 15 0009 7C 10 jl exit ;jump if < 0 16 000B 3C 09 cmp al,9d ;is it > 9d ? 17 000D 7F 0C jg exit ;yes, not dec digit 18 000F 98 cbw ;byte in AL to word in AX 19 20 ;(digit is now in AX) 21 ;Multiply number in BX by 10 decimal 22 0010 93 xchg ax,bx ;trade digit & number 23 0011 B9 000A mov cx,10d ;put 10 dec in CX 24 0014 F7 E1 mul cx ;number times 10 25 0016 93 xchg ax, bx ;trade number & digit 26 27 ;Add digit in AX to number in BX 28 0017 03 D8 add bx,ax ;add digit to number 29 0019 EB E8 jmp newchar ;get next digit 30 31 001B exit: 32 001B B4 4C mov ah,4ch ;DOS: terminate program 33 001D B0 00 mov al,0 ;return code will be 0 34 001F CD 21 int 21h 35 36 end start

Turbo Assembler Version 2.02 09/13/95 09:21:19 Page 2Symbol Table

Symbol Name Type Value

??DATE Text "09/13/95"??FILENAME Text "test2 "??TIME Text "09:21:19"??VERSION Number 0202@CODE Text _TEXT@CODESIZE Text 0@CPU Text 0101H@CURSEG Text _TEXT@DATA Text DGROUP@DATASIZE Text 0@FILENAME Text TEST2@MODEL Text 2@WORDSIZE Text 2EXIT Near _TEXT:001BNEWCHAR Near _TEXT:0003START Near _TEXT:0000

ASSEMBLY LANGUAGE page 16

Page 17: Content of Information Processing - Kwun Tong · Web viewopcode of machine codes. are frequently used instead of actual machine address. The assembly language must be translated, or

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.CODEstart: 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 delaywait1: 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 exitexit: mov ah, 4ch ;DOS: terminate program mov al, 0 ;return code will be 0 int 21h ;terminate the programend start

ASSEMBLY LANGUAGE page 17