basic tasm
TRANSCRIPT
Basic for Basic for Assembly Assembly
ProgrammingProgramming
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly language:
Assembly language is used for most programming because it is extremely difficult to program a microprocessor in its native, that is hexadecimal machine language.
Assembler:
An assembler is a program that converts software written in symbolic machine language (the source programme) into hexadecimal machine language (object programme).
The primary reason to use assembler is because development and modification are always difficult in machine language.
Assembly LanguageAssembly Language
Computer Organization and Assembly Language #2
Registers in 80x86 microprocessorRegisters in 80x86 microprocessor
General Purpose RegistersGeneral Purpose Registers• AX AccumulatorAX Accumulator• BX Base registerBX Base register• CX Counting registerCX Counting register• DX Data registerDX Data register
DLDLDHDH
CLCLCHCH
BLBLBHBH
ALALAHAH
15 8 7 015 8 7 0
AXAX
BXBX
CXCX
DXDX
AccumulatorAccumulator
BaseBase
CounterCounter
DataData
C. Vongchumyen 12 / 2003
Registers in 80x86 microprocessorRegisters in 80x86 microprocessor
Segment RegistersSegment Registers• CS Code Segment registerCS Code Segment register• DS Data Segment registerDS Data Segment register• SS Stack Segment registerSS Stack Segment register• ES Extra Segment registerES Extra Segment register
CS = F000HCS = F000HIP = 1234HIP = 1234HPhysical Address = F000Physical Address = F0000 0 HH
++ 1234 1234 HH
= = F1234 F1234 HH
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Registers in 80x86 microprocessorRegisters in 80x86 microprocessor
Special RegisterSpecial Register• BP Base Pointers registerBP Base Pointers register• SI Source Index registerSI Source Index register• DI Destiny Index registerDI Destiny Index register• SP Stack pointer registerSP Stack pointer register• IP Next Instruction Pointer registerIP Next Instruction Pointer register• F Flag registerF Flag register
O, D, I, T, S, Z, A, P, CO, D, I, T, S, Z, A, P, C
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Flag RegisterFlag Register
CFCFPFPFAFAFZFZFSFSFTFTFIFIFDFDFOFOF
11 10 9 8 7 6 4 2 11 10 9 8 7 6 4 2 00
OFOF Over FlowOver Flow Over flow from arithmetic Over flow from arithmetic DF DF DirectionDirection Inc or Dec for moving or compare stringInc or Dec for moving or compare stringIFIF InterruptInterrupt Enable or Disable all external interruptEnable or Disable all external interruptTFTF TrapTrap Enable or Disable single step operationEnable or Disable single step operationSFSF SignSign Sign bit for arithmetic number, 1 = negativeSign bit for arithmetic number, 1 = negativeZFZF ZeroZero Result is zeroResult is zeroAFAF AuxiliaryAuxiliary Carry out for bit 3 on 8-bit dataCarry out for bit 3 on 8-bit dataPFPF ParityParity Even or Odd parity of low-order, 8-bit dataEven or Odd parity of low-order, 8-bit dataCFCF CarryCarry Carry out from MSB, Left most of arithmeticCarry out from MSB, Left most of arithmetic
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Register Register MOV CX,DXMOV CX,DX
Immediate Immediate MOV AL,22hMOV AL,22h
Direct Direct MOV CX,[1234h]MOV CX,[1234h]
Register Indirect Register Indirect MOV AX,[BX] ; SI,DI,BP,BXMOV AX,[BX] ; SI,DI,BP,BX
Base Plus Index Base Plus Index MOV CX,[BX+ DI] ; BP, BX + SI, DIMOV CX,[BX+ DI] ; BP, BX + SI, DI
Register Relative Register Relative MOV AX, [BX+1000h]MOV AX, [BX+1000h]
Base Relative Plus Index Base Relative Plus Index MOV AX,[BX+SI+100h] MOV AX,[BX+SI+100h]
MOV AX,ARRAY[BX+SI]MOV AX,ARRAY[BX+SI]
Addressing ModeAddressing Mode
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly TemplateAssembly Template
Page 60,132Page 60,132TitleTitle Exasm1 skeleton of an assembly ProgramExasm1 skeleton of an assembly Program; ------------------------------------------------------------; ------------------------------------------------------------STACKSGSTACKSG SegmentSegment Para stack 'Stack'Para stack 'Stack'STACKSGSTACKSG EndsEnds DATASGDATASG SegmentSegment Para 'Data'Para 'Data'DATASGDATASG EndsEnds CODESGCODESG SegmentSegment Para 'Code'Para 'Code'BeginBegin ProcProc farfar
AssumeAssume CS:CODESG, DS:DATASG, SS:STACKSGCS:CODESG, DS:DATASG, SS:STACKSGmovmov ax,DATASGax,DATASGmovmov ds,axds,ax
. . .. . .movmov ax,4C00hax,4C00hintint 21h21h
BeginBegin endpendpCODESGCODESG EndsEnds
EndEnd Begin Begin
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Page 60,132Page 60,132• 60 Lines per page60 Lines per page• 132 Character per line on listing file ( .LST )132 Character per line on listing file ( .LST )
TitleTitle Exasm1 skeleton of an assembly Program Exasm1 skeleton of an assembly ProgramThis text will show on every page of listing file This text will show on every page of listing file
; -------------------------------------------; -------------------------------------------Comment, Start with ; ( Semi colon )Comment, Start with ; ( Semi colon )
Assembly TemplateAssembly Template
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly TemplateAssembly Template
STACKSGSTACKSG Segment Para stack 'Stack‘Segment Para stack 'Stack‘………………Defining Stack SegmentDefining Stack Segment………………..
STACKSGSTACKSG EndsEnds
NameName Segment Align Combine ‘class’ Segment Align Combine ‘class’
Alignment : Para Alignment : Para = start address in slot of 10H= start address in slot of 10HCombine : StackCombine : Stack = Combine segment with other when linked= Combine segment with other when linkedClass Type : ‘Stack’Class Type : ‘Stack’ = Group related segment when linking= Group related segment when linking
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly TemplateAssembly Template
DATASG DATASG SegmentSegment Para 'Data‘Para 'Data‘………………Defining Data SegmentDefining Data Segment………………..
DATASG DATASG EndsEnds
CODESG CODESG SegmentSegment Para 'Code‘Para 'Code‘………………Defining Code SegmentDefining Code Segment………………..
CODESG CODESG EndsEnds
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
BeginBegin ProcProc farfarAssumeAssume CS:CODESG, DS:DATASG, SS:STACKSGCS:CODESG, DS:DATASG, SS:STACKSGmovmov ax,DATASGax,DATASGmovmov ds,axds,ax
. . .. . .BeginBegin endpendp
Defining Procedure Defining Procedure BeginBegin ProcProc FarFarBeginBegin endpendp
Defining SegmentDefining SegmentAssume Assume CS:CODESG, DS:DATASG, SS:STACKSG CS:CODESG, DS:DATASG, SS:STACKSG
Set Address of Data SegmentSet Address of Data Segmentmovmov ax,DATASGax,DATASGmovmov ds,axds,ax
Assembly TemplateAssembly Template
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly TemplateAssembly Template
Set Program Entry PointSet Program Entry PointEndEnd Begin Begin
Exit CodeExit Codemovmov ax,4C00hax,4C00hintint 21h21h
• Using Dos Interrupt Function 4CHUsing Dos Interrupt Function 4CH• Return 00H as exit code to DOSReturn 00H as exit code to DOS
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly Template ( next one )Assembly Template ( next one )
.Model TINY.Model TINY
.STACK.STACK
.DATA.DATA
.CODE.CODE
ORG 100HORG 100Hmovmov ax,@dataax,@datamovmov ds,axds,ax………………movmov ax,4C00hax,4C00hintint 21h21h
endend
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assembly Template ( next one )Assembly Template ( next one )
640 KB640 KB64 KB Each64 KB Each64 KB Each64 KB EachLargeLarge
640 KB640 KB64 KB Each64 KB Each64 KB Each64 KB EachHugeHuge
640 KB640 KB64 KB64 KB64 KB Each64 KB EachMediumMedium
640 KB640 KB64 KB Each64 KB Each64 KB64 KBCompactCompact
128 KB128 KB64 KB64 KB64 KB64 KBSmallSmall
64 KB64 KB64 KB 64 KB TINYTINY
Max TotaMax TotallStackStackDataDataCodeCodeModeMode
. Model. Model
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
.STACK.STACKDefining Stack AreaDefining Stack Area
.DATA.DATADefining Data AreaDefining Data Area
.CODE.CODEDefining Code AreaDefining Code Area
EndEndEnd Program DirectiveEnd Program Directive
Assembly Template ( next one )Assembly Template ( next one )
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Set Address of Data segmentSet Address of Data segmentMovMov ax,@dataax,@dataMovMov ds,axds,ax
ORG 100HORG 100HFirst Op-Code locate at Offset 100HFirst Op-Code locate at Offset 100H( Required for .COM File )( Required for .COM File )
Assembly Template ( next one )Assembly Template ( next one )
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assemble and LinkingAssemble and Linking
Step of Assembly ProgrammingStep of Assembly Programming• Create Source CodeCreate Source Code• AssembleAssemble• LinkLink• Test and DebugTest and Debug
Tool For Assembly ProgrammingTool For Assembly Programming1.1. Text EditorText Editor2.2. Assembler ( TASM, MASM )Assembler ( TASM, MASM )3.3. Linker ( TLINK, LINK )Linker ( TLINK, LINK )4.4. Debugger ( Debug, Turbo Debugger)Debugger ( Debug, Turbo Debugger)
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Assemble and LinkingAssemble and Linking
C:\TASM\BIN>TASM test.asm ; /l = generate listing fileC:\TASM\BIN>TASM test.asm ; /l = generate listing fileTurbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland InternationalTurbo Assembler Version 4.1 Copyright (c) 1988, 1996 Borland International
Assembling file: test.asmAssembling file: test.asmError messages: NoneError messages: NoneWarning messages: NoneWarning messages: NonePasses: 1Passes: 1Remaining memory: 454kRemaining memory: 454k
C:\TASM\BIN>TLINK testC:\TASM\BIN>TLINK testTurbo Link Version 5.1 Copyright (c) 1992 Borland InternationalTurbo Link Version 5.1 Copyright (c) 1992 Borland International
.OBJ.OBJ
.EXE.EXE
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Frequency Error & WarningFrequency Error & Warning
.Model TINY.Model TINY
.STACK.STACK
.DATA.DATA
.CODE.CODE
ORG 100HORG 100Hmovmov ax,@dataax,@datamovmov ds,axds,ax………………movmov ax,4C00hax,4C00hintint 21h21h
**Fatal** test.asm(xx) Unexpected end of file encountered**Fatal** test.asm(xx) Unexpected end of file encountered
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Frequency Error & WarningFrequency Error & Warning
Warning: No stackWarning: No stack
.Model TINY.Model TINY
.CODE.CODE
ORG 100HORG 100Hmovmov ax,@dataax,@datamovmov ds,axds,ax………………movmov ax,4C00hax,4C00hintint 21h21h
endend
Computer Organization and Assembly Language #2 C. Vongchumyen 12 / 2003
Basic InstructionBasic Instruction
Instruction :Instruction : MOVMOV
Purpose : Purpose : Data transfer between memory cells, registers Data transfer between memory cells, registers and the accumulator.and the accumulator.
Syntax : Syntax : MOV Destiny, SourceMOV Destiny, Source
Example :Example : MOV AX,0006hMOV AX,0006hMOV BX,AXMOV BX,AXMOV AX,4C00hMOV AX,4C00h
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : LEALEA
Purpose : Purpose : To load the address of the source operator To load the address of the source operator
Syntax : Syntax : LEA destiny, sourceLEA destiny, source
Example :Example : LEA SI,VAR1LEA SI,VAR1equivalent to: equivalent to: MOV SI,OFFSET VAR1MOV SI,OFFSET VAR1
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : LAHFLAHF
Purpose : Purpose : It transfers the content of the flags to the AH It transfers the content of the flags to the AH register register
Syntax : Syntax : LAHFLAHF
Example :Example : LAHF LAHF
This instruction is useful to verify the state of the flags This instruction is useful to verify the state of the flags during the execution of our programduring the execution of our program
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : POPPOP
Purpose : Purpose : It recovers a piece of information from the It recovers a piece of information from the stack stack
Syntax : Syntax : POP destinyPOP destiny
Example :Example : POP AX POP AX
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : POPFPOPF
Purpose : Purpose : It extracts the flags stored on the stack It extracts the flags stored on the stack
Syntax : Syntax : POPFPOPF
Example :Example : POPFPOPF
BIT FLAG BIT FLAG 0 CF, 2 PF, 4 AF, 6 ZF, 7 SF, 8 TF, 9 IF, 10 DF, 0 CF, 2 PF, 4 AF, 6 ZF, 7 SF, 8 TF, 9 IF, 10 DF, 11 OF11 OF
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : PUSHPUSH
Purpose : Purpose : It places a word on the stack It places a word on the stack
Syntax : Syntax : PUSH sourcePUSH source
Example :Example : PUSH AXPUSH AX
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : PUSHFPUSHF
Purpose : Purpose : It places the value of the flags on the stack. It places the value of the flags on the stack.
Syntax : Syntax : PUSHFPUSHF
Example :Example : PUSHFPUSHF
This command decreases by 2 the value of the SP register This command decreases by 2 the value of the SP register and then the content of the flag register is transferred to the and then the content of the flag register is transferred to the stack, on the address indicated by SP.stack, on the address indicated by SP.The flags are left stored in memory on the same bits The flags are left stored in memory on the same bits indicated on the POPF command.indicated on the POPF command.
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : ANDAND
Purpose : Purpose : It performs the conjunction of the operators It performs the conjunction of the operators bit by bit. bit by bit.
Syntax : Syntax : AND destiny, sourceAND destiny, source
Example :Example : ANDAND AX,0FF00HAX,0FF00HANDAND AX,BXAX,BXANDAND AX,[BX]AX,[BX]
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : NEGNEG
Purpose : Purpose : It generates the complement to 2. It generates the complement to 2.
Syntax : Syntax : NEG destinyNEG destiny
Example :Example : NEG AXNEG AX
For example, if AX stores the value of 1234H, then:For example, if AX stores the value of 1234H, then:NEG AXNEG AXThis would leave the 0EDCCH value stored on the AX This would leave the 0EDCCH value stored on the AX register.register.
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : NOTNOT
Purpose : Purpose : It carries out the negation of the destiny It carries out the negation of the destiny operator bit by bit. operator bit by bit.
Syntax : Syntax : NOT destinyNOT destiny
Example :Example : NOT AXNOT AX
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : OROR
Purpose : Purpose : Logic inclusive OR Logic inclusive OR
Syntax : Syntax : OR destiny, sourceOR destiny, source
Example :Example : OROR AX,0FF00HAX,0FF00HOROR AX,BXAX,BXOROR AX,[BX]AX,[BX]
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : XORXOR
Purpose : Purpose : Logic exclusive OR Logic exclusive OR
Syntax : Syntax : XOR destiny, sourceXOR destiny, source
Example :Example : XORXOR AX,0FF00HAX,0FF00HXORXOR AX,BXAX,BXXORXOR AX,[BX]AX,[BX]
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : TESTTEST
Purpose : Purpose : It logically compares the operators It logically compares the operators
Syntax : Syntax : TEST destiny, sourceTEST destiny, source
Example :Example : TESTTEST AX,0FF00HAX,0FF00HTESTTEST AX,BXAX,BXTESTTEST AX,[BX]AX,[BX]
It performs a conjunction, bit by bit, of the operators, but It performs a conjunction, bit by bit, of the operators, but differing from AND, this instruction does not place the result differing from AND, this instruction does not place the result on the destiny operator, it only has effect on the state of the on the destiny operator, it only has effect on the state of the flags.flags.
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : ADC ADC
Purpose : Purpose : addition with carryaddition with carry
Syntax : Syntax : ADC destiny, sourceADC destiny, source
Example :Example : ADCADC AX,0FF00HAX,0FF00HADCADC AX,BXAX,BXADC AX,[BX]ADC AX,[BX]
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : ADD ADD
Purpose : Purpose : addition without carryaddition without carry
Syntax : Syntax : ADD destiny, sourceADD destiny, source
Example :Example : ADDADD AX,0FF00HAX,0FF00HADDADD AX,BXAX,BXADDADD AX,[BX]AX,[BX]
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : DIV DIV
Purpose : Purpose : Division without sign. Division without sign.
Syntax : Syntax : DIV sourceDIV source
Example :Example : DIV DIV BL ( 8 Bit )BL ( 8 Bit )AL = AX / BLAL = AX / BLAH = RemainderAH = Remainder
DIVDIV BX ( 16 Bit )BX ( 16 Bit )AX = DX:AX / BXAX = DX:AX / BXDX = RemainderDX = Remainder
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : IDIV IDIV
Purpose : Purpose : Division with sign. Division with sign.
Syntax : Syntax : IDIV sourceIDIV source
Example :Example : IDIV IDIV BL ( 8 Bit )BL ( 8 Bit )IDIVIDIV BX ( 16 Bit )BX ( 16 Bit )
basically consists on the same as the DIV instruction, basically consists on the same as the DIV instruction, and the only difference is that this one performs the and the only difference is that this one performs the operation with sign.operation with sign.For its results it used the same registers as the DIV For its results it used the same registers as the DIV instruction.instruction.
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : MUL MUL
Purpose : Purpose : Multiplication without sign. Multiplication without sign.
Syntax : Syntax : MUL sourceMUL source
Example :Example : MUL MUL CH ( 8 Bit )CH ( 8 Bit )AX = CH * ALAX = CH * AL
MULMUL BX ( 16 Bit )BX ( 16 Bit )DX:AX = BX * AXDX:AX = BX * AX
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Basic InstructionBasic Instruction
Instruction :Instruction : IMUL IMUL
Purpose : Purpose : Integer Multiplication with sign. Integer Multiplication with sign.
Syntax : Syntax : IMUL sourceIMUL source
Example :Example : IMUL IMUL CH ( 8 Bit )CH ( 8 Bit )AX = CH * ALAX = CH * AL
IMULIMUL BX ( 16 Bit )BX ( 16 Bit )DX:AX = BX * AXDX:AX = BX * AX
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Instruction :Instruction : SUB SUB
Purpose : Purpose : subtraction without carrysubtraction without carry
Syntax : Syntax : SUB destiny, sourceSUB destiny, source
Example :Example : SUBSUB AX,0FF00HAX,0FF00HSUBSUB AX,BXAX,BXSUBSUB AX,[BX]AX,[BX]
Basic InstructionBasic Instruction
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Instruction :Instruction : SBB SBB
Purpose : Purpose : subtraction without borrowsubtraction without borrow
Syntax : Syntax : SBB destiny, sourceSBB destiny, source
Example :Example : SBBSBB AX,0FF00HAX,0FF00HSBBSBB AX,BXAX,BXSBBSBB AX,[BX]AX,[BX]
Basic InstructionBasic Instruction
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2
Instruction :Instruction : SBB SBB
Purpose : Purpose : subtraction without borrowsubtraction without borrow
Syntax : Syntax : SBB destiny, sourceSBB destiny, source
Example :Example : SBBSBB AX,0FF00HAX,0FF00HSBBSBB AX,BXAX,BXSBBSBB AX,[BX]AX,[BX]
Basic InstructionBasic Instruction
C. Vongchumyen 12 / 2003Computer Organization and Assembly Language #2