asm guadalajara

Upload: mirandoariba

Post on 01-Jun-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Asm Guadalajara

    1/60

    Assembler Tutorial - 1996 Edition 1

    Assembler Tutorial

    University of GuadalajaraInformation Systems General Coordination.

    Culture and Entertainment eb

    June 12th 1995Copyright(C)1995-1996

    This is an introduction for people who want to programming in assemler language!

    Copyright " 1995-1996# $ugo %ere& %ere&! 'nyone may reproduce this document# in whole orin part# proided that (1) any copy or repulication of the entire document must show*niersity of +uadala,ara as the source# and must include this notice and (2) any other use ofthis material must reference this manual and *niersity of +uadala,ara# and the fact that thematerial is copyright y $ugo %ere& and is used y permission!

    'ssemler Tutorial1996 .dition

    Table of Contents

    1. Introdu!tion". #asi! Con!e$ts%. Assembler $ro&rammin&'. Assembler lan&ua&e instru!tions(. Interru$tions and file mana&in&6. )a!ros and $ro!edures*. +ro&ram e,am$les

    1. Introdu!tion

    Table of Contents

    1.1 ats ne/ in te Assembler material1." +resentation1.% y learn Assembler lan&ua&e1.' e need your o$inion

  • 8/9/2019 Asm Guadalajara

    2/60

    Assembler Tutorial - 1996 Edition 2

    1.1 ats ne/ in te Assembler material

    'fter of one year that we/e released the first 'ssemler material on-line! 0e/e receied a lotof e-mail where each people tal aout different aspects aout this material! 0e/e tried to put

    these comments and suggestions in this update assemler material! 0e hope that this new'ssemler material release reach to all people that they interest to learn the most importantlanguage for 34 %C!

    n this new assemler release includes

    ' complete chapter aout how to use deug program 4ore eample of the assemler material .ach section of this assemler material includes a lin file to ree 7n-line of Computing y 8ennis $owe inally# a search engine to loo for any topic or item related with this updated material!

    1." +resentation

    The document you are looing at# has the primordial function of introducing you to assemlylanguage programming# and it has een thought for those people who hae neer wored withthis language!

    The tutorial is completely focused towards the computers that function with processors of the6 family of ntel# and considering that the language ases its functioning on the internalresources of the processor# the descried eamples are not compatile with any otherarchitecture!

    The information was structured in units in order to allow easy access to each of the topics and

    facilitate the following of the tutorial!

    n the introductory section some of the elemental concepts regarding computer systems arementioned# along with the concepts of the assemly language itself# and continues with thetutorial itself!

    1.% y learn assembler lan&ua&e

    The first reason to wor with assemler is that it proides the opportunity of nowing more theoperation of your %C# which allows the deelopment of software in a more consistent manner!

    The second reason is the total control of the %C which you can hae with the use of the

    assemler!

    'nother reason is that the assemly programs are :uicer# smaller# and hae larger capacitiesthan ones created with other languages!

    ;astly# the assemler allows an ideal optimi&ation in programs# e it on their si&e or on theireecution!

    1.' e need your o$inion

    7ur goal is offers you easier way to learn yourself assemler language!

  • 8/9/2019 Asm Guadalajara

    3/60

    Assembler Tutorial - 1996 Edition =

    ". #asi! Con!e$ts

    Table of Contents

    ".1 #asi! des!ri$tion of a !om$uter system."." Assembler lan&ua&e #asi! !on!e$ts".% Usin& debu& $ro&ram

    ".1 #asi! des!ri$tion of a !om$uter system.

    This section has the purpose of giing a rief outline of the main components of a computersystem at a asic leel# which will allow the user a greater understanding of the concepts whichwill e dealt with throughout the tutorial!

    Computer System.

    0e call computer system to the complete configuration of a computer# including the peripheralunits and the system programming which mae it a useful and functional machine for adetermined tas!

    Table of Contents

    ".1.1 Central +ro!essor".1." Central )emory".1.% In$ut and 0ut$ut Units

    ".1.' Au,iliary )emory Units

    ".1.1 Central +ro!essor.

    This part is also nown as central processing unit or C%*# which in turn is made y the controlunit and the arithmetic and logic unit! ts functions consist in reading and writing the contents ofthe memory cells# to forward data etween memory cells and special registers# and decode andeecute the instructions of a program! The processor has a series of memory cells which areused ery often and thus# are part of the C%*! These cells are nown with the name of registers!' processor may hae one or two do&en of these registers! The arithmetic and logic unit of the

    C%* reali&es the operations related with numeric and symolic calculations!

    Typically these units only hae capacity of performing ery elemental operations such as theaddition and sutraction of two whole numers# whole numer multiplication and diision#handling of the registers/ its and the comparison of the content of two registers! %ersonalcomputers can e classified y what is nown as word si&e# this is# the :uantity of its which the

    processor can handle at a time!

    ".1." Central )emory.

    t is a group of cells# now eing faricated with semi-conductors# used for general processes#

    such as the eecution of programs and the storage of information for the operations!.ach one of these cells may contain a numeric alue and they hae the property of eingaddressale# this is# that they can distinguish one from another y means of a uni:ue numer oran address for each cell!

  • 8/9/2019 Asm Guadalajara

    4/60

    Assembler Tutorial - 1996 Edition >

    The generic name of these memories is ?andom 'ccess 4emory or ?'4! The maindisadantage of this type of memory is that the integrated circuits lose the information they haestored when the electricity flow is interrupted! This was the reason for the creation of memorieswhose information is not lost when the system is turned off! These memories receie the name

    of ?ead 7nly 4emory or ?74!

    ".1.% In$ut and 0ut$ut Units.

    n order for a computer to e useful to us it is necessary that the processor communicates withthe eterior through interfaces which allow the input and output of information from the

    processor and the memory! Through the use of these communications it is possile to introduceinformation to e processed and to later isuali&e the processed data!

    @ome of the most common input units are eyoards and mice! The most common output unitsare screens and printers!

    ".1.' Au,iliary )emory Units.

    @ince the central memory of a computer is costly# and considering today/s applications it is alsoery limited! Thus# the need to create practical and economical information storage systemsarises! 3esides# the central memory loses its content when the machine is turned off# thereforemaing it inconenient for the permanent storage of data!

    These and other inconenience gie place for the creation of peripheral units of memory whichreceie the name of auiliary or secondary memory! 7f these the most common are the tapesand magnetic discs!

    The stored information on these magnetic media means receie the name of files! ' file is madeof a ariale numer of registers# generally of a fied si&e the registers may containinformation or programs!

    "." Assembler lan&ua&e #asi! !on!e$ts

    Table of Contents

    ".".1 Information in te !om$uters"."." ata re$resentation metods

    ".".1 Information in te !om$uter

    Table of Contents

    ".".1.1 Information units".".1." 2umeri! systems".".1.% Convertin& binary numbers to de!imal".".1.' Convertin& de!imal numbers to binary

    ".".1.( 3e,ade!imal system

  • 8/9/2019 Asm Guadalajara

    5/60

    Assembler Tutorial - 1996 Edition 5

    ".".1.1 Information Units

    n order for the %C to process information# it is necessary that this information e in specialcells called registers! The registers are groups of or 16 flip-flops!

    ' flip-flop is a deice capale of storing two leels of oltage# a low one# regularly A!5 olts#and another one# commonly of 5 olts! The low leel of energy in the flip-flop is interpreted asoff or A# and the high leel as on or 1! These states are usually nown as its# which are thesmallest information unit in a computer!

    ' group of 16 its is nown as word a word can e diided in groups of its called ytes# andthe groups of > its are called niles!

    ".".1." 2umeri! systems

    The numeric system we use daily is the decimal system# ut this system is not conenient for

    machines since the information is handled codified in the shape of on or off its this way ofcodifying taes us to the necessity of nowing the positional calculation which will allow us toepress a numer in any ase where we need it!

    t is possile to represent a determined numer in any ase through the following formula

    0here n is the position of the digit eginning from right to left and numering from &ero! 8 isthe digit on which we operate and 3 is the used numeric ase!

    ".".1.% Convertin& binary numbers to de!imals

    0hen woring with assemly language we come on the necessity of conerting numers fromthe inary system# which is used y computers# to the decimal system used y people!

    The inary system is ased on only two conditions or states# e it on(1) or off(A)# thus its ase istwo!

    or the conersion we can use the positional alue formula

    or eample# if we hae the inary numer of 1AA11# we tae each digit from right to left andmultiply it y the ase# eleated to the new position they are

    3inary 1 1 A A 1

    8ecimal 1B2A D 1B21 D AB22 D AB2= D 1B2>

    E 1 D 2 D A D A D 16 E 19 decimal!

    The character is used in computation as an eponent symol and the B character is used torepresent multiplication!

  • 8/9/2019 Asm Guadalajara

    6/60

    Assembler Tutorial - 1996 Edition 6

    ".".1.' Convertin& de!imal numbers to binary

    There are seeral methods to conert decimal numers to inary only one will e analy&edhere! Faturally a conersion with a scientific calculator is much easier# ut one cannot always

    count with one# so it is conenient to at least now one formula to do it!

    The method that will e eplained uses the successie diision of two# eeping the residue as ainary digit and the result as the net numer to diide!

    ;et us tae for eample the decimal numer of >=!

    43/2 = 21 and its residue is 1

    21/2 = 10 and its residue is 1

    10/2 = 5 and its residue is 0

    5/2 = 2 and its residue is 1

    2/2 = 1 and its residue is 0

    1/2 = 0 and its residue is 1

    3uilding the numer from the ottom #we get that the inary result is 1A1A11

    ".".1.( 3e,ade!imal system

    7n the headecimal ase we hae 16 digits which go from A to 9 and from the letter ' to the #these letters represent the numers from 1A to 15! Thus we count A#1#2#=#>#5#6#G##9#'#3#C#8#.#and !

    The conersion etween inary and headecimal numers is easy! The first thing done to do aconersion of a inary numer to a headecimal is to diide it in groups of > its# eginningfrom the right to the left! n case the last group# the one most to the left# is under > its# themissing places are filled with &eros!

    Taing as an eample the inary numer of 1A1A11# we diide it in > its groups and we are leftwith

    10 1011

    illing the last group with &eros (the one from the left)

    0010 1011

    'fterwards we tae each group as an independent numer and we consider its decimal alue 0010 = 2 ; 1011 = 11

    3ut since we cannot represent this headecimal numer as 211 ecause it would e an error# wehae to sustitute all the alues greater than 9 y their respectie representation in headecimal#with which we otain

    23$# where the $ represents the headecimal ase!

    n order to conert a headecimal numer to inary it is only necessary to inert the steps thefirst headecimal digit is taen and conerted to inary# and then the second# and so on!

  • 8/9/2019 Asm Guadalajara

    7/60

    Assembler Tutorial - 1996 Edition G

    "."." ata re$resentation metods in a !om$uter.

    Table of Contents

    ".".".1.ASCII !ode"."."." #C metod".".".% 4loatin& $oint re$resentation

    ".".".1 ASCII !ode

    '@C is an acronym of 'merican @tandard Code for nformation nterchange! This codeassigns the letters of the alphaet# decimal digits from A to 9 and some additional symols a

    inary numer of G its# putting the th it in its off state or A! This way each letter# digit or

    special character occupies one yte in the computer memory!0e can osere that this method of data representation is ery inefficient on the numeric aspect#since in inary format one yte is not enough to represent numers from A to 255# ut on theother hand with the '@C code one yte may represent only one digit! 8ue to this inefficiency#the '@C code is mainly used in the memory to represent tet!

    "."."." #C )etod

    3C8 is an acronym of 3inary Coded 8ecimal! n this notation groups of > its are used torepresent each decimal digit from A to 9! 0ith this method we can represent two digits per yte

    of information!.en when this method is much more practical for numer representation in the memorycompared to the '@C code# it still less practical than the inary since with the 3C8 methodwe can only represent digits from A to 99! 7n the other hand in inary format we can representall digits from A to 255!

    This format is mainly used to represent ery large numers in mercantile applications since itfacilitates operations aoiding mistaes!

    ".".".% 4loatin& $oint re$resentation

    This representation is ased on scientific notation# this is# to represent a numer in two parts itsase and its eponent!

    's an eample# the numer 12=>AAA# can e represented as 1!12=B1A6# in this last notation theeponent indicates to us the numer of spaces that the decimal point must e moed to the rightto otain the original result!

    n case the eponent was negatie# it would e indicating to us the numer of spaces that thedecimal point must e moed to the left to otain the original result!

  • 8/9/2019 Asm Guadalajara

    8/60

    Assembler Tutorial - 1996 Edition

    ".% Usin& ebu& $ro&ram

    Table of Contents

    ".%.1 +ro&ram !reation $ro!ess".%." C+U re&isters".%.% ebu& $ro&ram".%.' Assembler stru!ture".%.( Creatin& basi! assembler $ro&ram".%.6 Storin& and loadin& te $ro&rams".%.* )ore debu& $ro&ram e,am$les

    ".%.1 +ro&ram !reation $ro!ess

    or the creation of a program it is necessary to follow fie steps

    8esign of the algorithm# stage the prolem to e soled is estalished and the est solution

    is proposed# creating schematic diagrams used for the etter solution proposal!

    Coding the algorithm# consists in writing the program in some programming language

    assemly language in this specific case# taing as a ase the proposed solution on the priorstep!

    Translation to machine language# is the creation of the o,ect program# in other words# the

    written program as a se:uence of &eros and ones that can e interpreted y the processor!

    Test the program# after the translation the program into machine language# eecute the

    program in the computer machine!

    The last stage is the elimination of detected faults on the program on the test stage! The

    correction of a fault normally re:uires the repetition of all the steps from the first or second!

    ".%." C+U 5e&isters

    The C%* has > internal registers# each one of 16 its! The first four# 'H# 3H# CH# and 8H aregeneral use registers and can also e used as it registers# if used in such a way it is necessaryto refer to them for eample as '$ and ';# which are the high and low ytes of the 'Hregister! This nomenclature is also applicale to the 3H# CH# and 8H registers!

    The registers nown y their specific names

    AX A!!umulator

    BX Base re&ister

    CX Countin& re&ister

    DX Data re&ister

    DS Data Se&ment re&ister

    ES E,tra Se&ment re&ister

    SS #attery se&ment re&ister

    CS Code Se&ment re&ister

    BP Base Pointers re&ister

    SI Sour!e Inde, re&ister

    DI Destiny Inde, re&ister

    SP #attery $ointer re&ister

    IP 2e,t Instru!tion Pointer re&ister

    F Fla& re&ister

  • 8/9/2019 Asm Guadalajara

    9/60

    Assembler Tutorial - 1996 Edition 9

    ".%.% ebu& $ro&ram

    To create a program in assemler two options eist# the first one is to use the T'@4 or Turo'ssemler# of 3orland# and the second one is to use the deugger - on this first section we will

    use this last one since it is found in any %C with the 4@-87@# which maes it aailale to anyuser who has access to a machine with these characteristics!

    8eug can only create files with a !C74 etension# and ecause of the characteristics of theseinds of programs they cannot e larger that 6> # and they also must start with displacement#offset# or A1AA$ memory direction inside the specific segment!

    8eug proides a set of commands that lets you perform a numer of useful operations

    A 'ssemle symolic instructions into machine code

    D 8isplay the contents of an area of memory

    E .nter data into memory# eginning at a specific location

    G ?un the eecutale program in memory N Fame a program

    P %roceed# or eecute a set of related instructions

    Q Iuit the deug program

    R 8isplay the contents of one or more registers

    T Trace the contents of one instruction

    U *nassemled machine code into symolic code

    W 0rite a program onto dis

    t is possile to isuali&e the alues of the internal registers of the C%* using the 8eug

    program! To egin woring with 8eug# type the following prompt in your computer

    C:/>Debug [Enter]

    7n the net line a dash will appear# this is the indicator of 8eug# at this moment theinstructions of 8eug can e introduced using the following command

    -r[Enter]

    AX=0000 X=0000 CX=0000 DX=0000 !"=##EE "=0000 !$=0000 D$=0000D!=0D%2 E!=0D%2 !!=0D%2 C!=0D%2 $"=0100 &' E$ "( &) &A "* &C0D%2:0100 2E C!:0D%2:0101 +03ED3D#00 C," .E ". [D#D3]00 C!:D#D3=03

    'll the contents of the internal registers of the C%* are displayed an alternatie of iewingthem is to use the r command using as a parameter the name of the register whose alue wantsto e seen! or eample

    -rbX 0000:

    This instruction will only display the content of the 3H register and the 8eug indicatorchanges from - to

    0hen the prompt is lie this# it is possile to change the alue of the register which was seen ytyping the new alue and K.nterL# or the old alue can e left y pressing K.nterL without typingany other alue!

  • 8/9/2019 Asm Guadalajara

    10/60

    Assembler Tutorial - 1996 Edition 1A

    ".%.' Assembler stru!ture

    n assemly language code lines hae two parts# the first one is the name of the instructionwhich is to e eecuted# and the second one are the parameters of the command! or eample

    add a b

    $ere add is the command to e eecuted# in this case an addition# and ah as well as h arethe parameters! or eample

    a6 25

    n the aoe eample# we are using the instruction mov# it means moe the alue 25 to alregister!

    The name of the instructions in this language is made of two# three or four letters! Theseinstructions are also called mnemonic names or operation codes# since they represent a functionthe processor will perform!

    @ometimes instructions are used as follows

    add a6[170]

    The racets in the second parameter indicate to us that we are going to wor with the contentof the memory cell numer 1GA and not with the 1GA alue# this is nown as direct addressing!

    ".%.( Creatin& basi! assembler $ro&ram

    The first step is to initiate the 8eug# this step only consists of typing deugK.nterL on the

    operatie system prompt!To assemle a program on the 8eug# the a (assemle) command is used when this commandis used# the address where you want the assemling to egin can e gien as a parameter# if the

    parameter is omitted the assemling will e initiated at the locality specified y C@%# usually

    A1AAh# which is the locality where programs with !C74 etension must e initiated! 'nd it wille the place we will use since only 8eug can create this specific type of programs!

    .en though at this moment it is not necessary to gie the a command a parameter# it isrecommendale to do so to aoid prolems once the C@% registers are used# therefore we type

    a 100[enter] a0002[enter]

    b0004[enter]add ab[enter]n8[enter][enter]

    0hat does the program doM# moe the alue AAA2 to the a register# moe the alue AAA> to the register# add the contents of the a and registers# the instruction# no operation# to finish theprogram!

    n the deug program! 'fter to do this# appear on the screen some lie the follow lines

    C:9>debug-a 1000D%2:0100 a0002

    0D%2:0103 b00040D%2:010% add ab0D%2:010+ n80D%2:010

  • 8/9/2019 Asm Guadalajara

    11/60

    Assembler Tutorial - 1996 Edition 11

    Type the command t (trace)# to eecute each instruction of this program# eample

    -t

    AX=0002 X=0000 CX=0000 DX=0000 !"=##EE "=0000 !$=0000 D$=0000D!=0D%2 E!=0D%2 !!=0D%2 C!=0D%2 $"=0103 &' E$ "( &) &A "* &C0D%2:0103 0400 ,*' X0004

  • 8/9/2019 Asm Guadalajara

    12/60

    Assembler Tutorial - 1996 Edition 12

    0C1:010A

    To otain the length of a program the h command is used# since it will show us the additionand sutraction of two numers in headecimal! To otain the length of ours# we gie it as

    parameters the alue of our program/s final address (1A')# and the program/s initial address(1AA)! The first result the command shows us is the addition of the parameters and the second isthe sutraction!

    - 10a 100020a 000a

    The n command allows us to name the program!

    -n test

  • 8/9/2019 Asm Guadalajara

    13/60

    Assembler Tutorial - 1996 Edition 1=

    % Assembler $ro&rammin&

    Table of Contents

    %.1 #uildin& Assembler $ro&rams%." Assembly $ro!ess%.% )ore assembler $ro&rams%.' Ty$es of instru!tions%.( Cli! ere to &et more assembler $ro&rams

    %.1 #uildin& Assembler $ro&rams

    Table of Contents

    %.1.1 2eeded soft/are%.1." Assembler +ro&rammin&

    %.1.1 2eeded soft/are

    n order to e ale to create a program# seeral tools are needed

    irst an editor to create the source program! @econd a compiler# which is nothing more than aprogram that translates the source program into an o,ect program! 'nd third# a liner thatgenerates the eecutale program from the o,ect program!

    The editor can e any tet editor at hand# and as a compiler we will use the T'@4 macroassemler from 3orland# and as a liner we will use the Tlin program!

    The etension used so that T'@4 recogni&es the source programs in assemler is !'@4 oncetranslated the source program# the T'@4 creates a file with the !73J etension# this filecontains an intermediate format of the program# called lie this ecause it is not eecutaleyet ut it is not a program in source language either anymore! The liner generates# from a !73Jor a comination of seeral of these files# an eecutale program# whose etension usually is!.H. though it can also e !C74# depending of the form it was assemled!

    %.1." Assembler +ro&rammin&

    To uild assemler programs using T'@4 programs is a different program structure than fromusing deug program!

    t/s important to include the following assemler directies

    ,*DE( !,A((

    'ssemler directie that defines the memory model to use in the program

    C*DE

    'ssemler directie that defines the program instructions

    !.AC@

    'ssemler directie that reseres a memory space for program instructions in the stac

    E&D

    'ssemler directie that finishes the assemler program

  • 8/9/2019 Asm Guadalajara

    14/60

  • 8/9/2019 Asm Guadalajara

    15/60

    Assembler Tutorial - 1996 Edition 15

    Assembly $ro!ess.

    Se&ments

    Table of symbols

    SEGMENTS

    The architecture of the 6 processors forces to the use of memory segments to manage theinformation# the si&e of these segments is of 6>!

    The reason of eing of these segments is that# considering that the maimum si&e of a numerthat the processor can manage is gien y a word of 16 its or register# it would not e possileto access more than 655=6 localities of memory using only one of these registers# ut now# if the%C/s memory is diided into groups or segments# each one of 655=6 localities# and we use anaddress on an eclusie register to find each segment# and then we mae each address of a

    specific slot with two registers# it is possile for us to access a :uantity of >29>96G296 ytes ofmemory# which is# in the present day# more memory than what we will see installed in a %C!

    n order for the assemler to e ale to manage the data# it is necessary that each piece ofinformation or instruction e found in the area that corresponds to its respectie segments! Theassemler accesses this information taing into account the locali&ation of the segment# gien

    y the 8@# .@# @@ and C@ registers and inside the register the address of the specified piece ofinformation! t is ecause of this that when we create a program using the 8eug on each linethat we assemle# something lie this appears

    1C0:0102 ,*' AXX

    0here the first numer# 1C3A# corresponds to the memory segment eing used# the second onerefers to the address inside this segment# and the instructions which will e stored from thataddress follow!

    The way to indicate to the assemler with which of the segments we will wor with is withthe !C78.# !8'T' and !@T'CN directies!

    The assemler ad,usts the si&e of the segments taing as a ase the numer of ytes eachassemled instruction needs# since it would e a waste of memory to use the whole segments!or eample# if a program only needs 1A to store data# the data segment will only e of 1Aand not the 6> it can handle!

    SYMBOLSCA!T

    .ach one of the parts on code line in assemler is nown as toen# for eample on the code line

    ,*' AX'ar

    we hae three toens# the 47O instruction# the 'H operator# and the O'? operator! 0hat theassemler does to generate the 73J code is to read each one of the toens and loo for it on aninternal e:uialence chart nown as the resered words chart# which is where all the nemonicmeanings we use as instructions are found!

    ollowing this process# the assemler reads 47O# loos for it on its chart and identifies it as a

    processor instruction! ;iewise it reads 'H and recogni&es it as a register of the processor# utwhen it loos for the Oar toen on the resered words chart# it does not find it# so then it loosfor it on the symols chart which is a tale where the names of the ariales# constants and

  • 8/9/2019 Asm Guadalajara

    16/60

    Assembler Tutorial - 1996 Edition 16

    laels used in the program where their addresses on memory are included and the sort of data itcontains# are found!

    @ometimes the assemler comes on a toen which is not defined on the program# therefore whatit does in these cased is to pass a second time y the source program to erify all references to

    that symol and place it on the symols chart!

    There are symols which the assemler will not find since they do not elong to that segmentand the program does not now in what part of the memory it will find that segment# and at thistime the liner comes into action# which will create the structure necessary for the loader so thatthe segment and the toen e defined when the program is loaded and efore it is eecuted!

    %.% )ore assembler $ro&rams

    Another example

    first step

    use any editor program to create the source file! Type the following lines

    ;ea86e11de6 sa66sta

  • 8/9/2019 Asm Guadalajara

    17/60

    Assembler Tutorial - 1996 Edition 1G

    C:9>

    fifth step

    .ecute the eecutale program

    C:9>ee11[enter]FC:9>

    This assemler program shows the asteris character on the computer screen!

    %.' Ty$es of instru!tions.

    Table of Contents

    %.'.1 ata movement%.'." 7o&i! and aritmeti! o$erations%.'.% 8um$s loo$s and $ro!edures

    %.'.1 ata movement

    n any program it is necessary to moe the data in the memory and in the C%* registers there

    are seeral ways to do this it can copy data in the memory to some register# from register toregister# from a register to a stac# from a stac to a register# to transmit data to eternal deicesas well as ice ersa!

    This moement of data is su,ect to rules and restrictions! The following are some of them

    t is not possile to moe data from a memory locality to another directly it is necessary to

    first moe the data of the origin locality to a register and then from the register to thedestiny locality!

    t is not possile to moe a constant directly to a segment register it first must e moed to

    a register in the C%*!

    t is possile to moe data locs y means of the movs instructions# which copies a chain ofytes or words movsbwhich copies n ytes from a locality to another and movswcopies nwords from a locality to another! The last two instructions tae the alues from the definedaddresses y 8@@ as a group of data to moe and .@8 as the new locali&ation of the data!

    To moe data there are also structures called atteries# where the data is introduced with thepush instruction and are etracted with the pop instruction! n a stac the first data to eintroduced is the last one we can tae# this is# if in our program we use these instructions

    PUSH AX

    PUSH BX

    PUSH CX

    To return the correct alues to each register at the moment of taing them from the stac it isnecessary to do it in the following order

    POP CX

  • 8/9/2019 Asm Guadalajara

    18/60

    Assembler Tutorial - 1996 Edition 1

    POP BX

    POP AX

    or the communication with eternal deices the out command is used to send information to a

    port and the in command to read the information receied from a port!The synta of the out command is

    OUT DX,AX

    0here 8H contains the alue of the port which will e used for the communication and 'Hcontains the information which will e sent!

    The synta of the in command is

    IN AX,DX

    0here 'H is the register where the incoming information will e ept and 8H contains the

    address of the port y which the information will arrie!

    %.'." 7o&i! and aritmeti! o$erations

    The instructions of the logic operations are and# not# or andxor! These wor on the its of theiroperators! To erify the result of the operations we turn to the cmpand test instructions! Theinstructions used for the algeraic operations are to add# to sutractsub# to multiply muland todiide div!

    'lmost all the comparison instructions are ased on the information contained in the flagregister! Formally the flags of this register which can e directly handled y the programmer arethe data direction flag 8# used to define the operations aout chains!

    'nother one which can also e handled is the flag y means of the stiand cliinstructions# toactiate and deactiate the interruptions!

    aaei

    %.'.% 8um$s loo$s and $ro!edures

    The unconditional ,umps in a written program in assemler language are gien y the jmpinstruction a ,ump is to moes the flow of the eecution of a program y sending the control tothe indicated address!

    ' loop# nown also as iteration# is the repetition of a process a certain numer of times until acondition is fulfilled! These loops are used!

  • 8/9/2019 Asm Guadalajara

    19/60

    Assembler Tutorial - 1996 Edition 19

    ' Assembler lan&ua&e Instru!tions

    Table of Contents

    '.1 Transfer instru!tions'." 7oadin& instru!tions'.% Sta! instru!tions'.' 7o&i! instru!tions'.( Aritmeti! instru!tions'.6 8um$ instru!tions'.* Instru!tions for !y!les: loo$'.; Countin& Instru!tions'.9 Com$arison Instru!tions'.1< 4la& Instru!tions

    '.1 Transfer instru!tions

    They are used to moe the contents of the operators! .ach instruction can e used with differentmodes of addressing!

    ,*',*'! ,*'! ,*'!

    47O F@T?*CT7F

    Purpose 8ata transfer etween memory cells# registers and the accumulator!

    Syntax

    ,*' Destin? !urC option of the 21h interruption!

    8estiny memory @ource accumulator 8estiny accumulator @ource memory8estiny segment register @ource memoryPregister8estiny memoryPregister @ource segment register

    8estiny register @ource register 8estiny register @ource memory8estiny memory @ource register 8estiny register @ource immediate data8estiny memory @ource immediate data

  • 8/9/2019 Asm Guadalajara

    20/60

    Assembler Tutorial - 1996 Edition 2A

    47O@ (47O@3) (47O@0) F@T?*CT7F

    Purpose To moe yte or word chains from the source# addressed y @# to the destinyaddressed y 8!

    Syntax

    ,*'!

    This command does not need parameters since it taes as source address the content of the@ register and as destination the content of 8! The following se:uence of instructionsillustrates this

    ,*' !$ *##!E. 'A1,*' D$ *##!E. 'A2,*'!

    irst we initiali&e the alues of @ and 8 with the addresses of the O'?1 and O'?2ariales respectiely# then after eecuting 47O@ the content of O'?1 is copied ontoO'?2!

    The 47O@3 and 47O@0 are used in the same way as 47O@# the first one moes oneyte and the second one moes a word!

    '." 7oadin& instru!tions

    They are specific register instructions! They are used to load ytes or chains of ytes onto aregister!

    (*D! (*D! (*D! (AG#(D! (EA(E!

    ;78@ (;78@3) (;78@0) F@T?*CT7F

    Purpose To load chains of a yte or a word into the accumulator!

    Syntax(*D!

    This instruction taes the chain found on the address specified y @# loads it to the '; (or'H) register and adds or sutracts # depending on the state of 8# to @ if it is a ytestransfer or if it is a words transfer!

    ,*' !$ *##!E. 'A1(*D!

    The first line loads the O'?1 address on @ and the second line taes the content of thatlocality to the '; register!

    The ;78@3 and ;78@0 commands are used in the same way# the first one loads a yteand the second one a word (it uses the complete 'H register)!

  • 8/9/2019 Asm Guadalajara

    21/60

    Assembler Tutorial - 1996 Edition 21

    ;'$ F@T?*CT7F

    Purpose t transfers the content of the flags to the '$ register!

    Syntax

    (AG#

    This instruction is useful to erify the state of the flags during the eecution of our program!The flags are left in the following order inside the register

    @ Q MM ' MM % MM C

    The MM means that there will e an undefined alue in those its!

    ;8@ F@T?*CT7F

    Purpose To load the register of the data segment

    Syntax

    (D! destin? sur

  • 8/9/2019 Asm Guadalajara

    22/60

  • 8/9/2019 Asm Guadalajara

    23/60

    Assembler Tutorial - 1996 Edition 2=

    These localities are the same for the %*@$ command!7nce the transference is done the @% register is increased y 2# diminishing the si&e of the

    stac!

    %*@$ F@T?*CT7F

    Purpose t places a word on the stac!

    Syntax

    "H!G sur

  • 8/9/2019 Asm Guadalajara

    24/60

    Assembler Tutorial - 1996 Edition 2>

    'F8 F@T?*CT7F

    Purpose t performs the con,unction of the operators it y it!

    Syntax

    A&D destin? sur

  • 8/9/2019 Asm Guadalajara

    25/60

    Assembler Tutorial - 1996 Edition 25

    7? F@T?*CT7F

    Purpose ;ogic inclusie 7?

    Syntax

    * destin? sur

  • 8/9/2019 Asm Guadalajara

    26/60

    Assembler Tutorial - 1996 Edition 26

    They are used to perform arithmetic operations on the operators!

    ADC ADDD$' $D$'

    ,H( $,H(! !H

    '8C F@T?*CT7F

    Purpose Cartage addition

    Syntax

    ADC destin? sur

  • 8/9/2019 Asm Guadalajara

    27/60

    Assembler Tutorial - 1996 Edition 2G

    8O F@T?*CT7F

    Purpose 8iision with sign!

    Syntax

    $D$' sur

  • 8/9/2019 Asm Guadalajara

    28/60

    Assembler Tutorial - 1996 Edition 2

    @*3 F@T?*CT7F

    Purpose @utraction!

    Syntax!H destin? sur

  • 8/9/2019 Asm Guadalajara

    29/60

    Assembler Tutorial - 1996 Edition 29

    J3 (JF'.) F@T?*CT7F

    Purpose Conditional ,ump!

    SyntaxJ 6abe6

    t ,umps if it is down# if it is not # or if it is the e:ual!The ,ump is done if C is actiated!

    J3. (JF') F@T?*CT7F

    Purpose Conditional ,ump!

    Syntax

    JE 6abe6

    t ,umps if it is down# the e:ual# or if it is not !The ,ump is done if C is actiated or if Q is actiated# that any of them e e:ual to 1!

    J. (JQ) F@T?*CT7F

    Purpose Conditional ,ump!

    Syntax

    JE 6abe6

    t ,umps if it is the e:ual or if it is &ero!The ,ump is done if Q is actiated!

    JF. (JFQ) F@T?*CT7F

    Purpose Conditional ,ump!

    Syntax

    J&E 6abe6

    t ,umps if it is not e:ual or &ero!The ,ump will e done if Q is deactiated!

    J+ (JF;.) F@T?*CT7F

    Purpose Conditional ,ump# and the sign is taen into account!

    Syntax

    JI 6abe6

    t ,umps if it is larger# if it is not larger or e:ual!

    The ,ump occurs if Q E A or if 7 E @!

  • 8/9/2019 Asm Guadalajara

    30/60

    Assembler Tutorial - 1996 Edition =A

    J+. (JF;) F@T?*CT7F

    Purpose Conditional ,ump# and the sign is taen into account!

    SyntaxJIE 6abe6

    t ,umps if it is larger or less than# or e:ual to!The ,ump is done if @ E 7

    J; (JF+.) F@T?*CT7F

    Purpose Conditional ,ump# and the sign is taen into account!

    Syntax

    J( 6abe6

    t ,umps if it is less than or if it is not larger than or e:ual to!The ,ump is done if @ is different than 7!

    J;. (JF+) F@T?*CT7F

    Purpose Conditional ,ump# and the sign is taen into account!

    Syntax

    J(E 6abe6

    t ,umps if it is less than or e:ual to# or if it is not larger!The ,ump is done if Q E 1 or if @ is different than 7!

    JC F@T?*CT7F

    Purpose Conditional ,ump# and the flags are taen into account!

    Syntax

    JC 6abe6

    t ,umps if there is cartage! The ,ump is done if C E 1

    JFC F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    Syntax

    J&C 6abe6

    t ,umps if there is no cartage!The ,ump is done if C E A!

  • 8/9/2019 Asm Guadalajara

    31/60

    Assembler Tutorial - 1996 Edition =1

    JF7 F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    SyntaxJ&* 6abe6

    t ,umps if there is no oerflow! The ,ump is done if 7 E A!

    JF% (J%7) F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    Syntax

    J&" 6abe6t ,umps if there is no parity or if the parity is uneen!The ,ump is done if % E A!

    JF@ F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    Syntax

    J&" 6abe6

    t ,umps if the sign is deactiated! The ,ump is done if @ E A!

    J7 F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    Syntax

    J* 6abe6

    t ,umps if there is oerflow! The ,ump is done if 7 E 1!

    J% (J%.) F@T?*CT7F

    Purpose Conditional ,ump# the state of the flags is taen into account!

    Syntax

    J" 6abe6

    t ,umps if there is parity or if the parity is een!The ,ump is done if % E 1!

  • 8/9/2019 Asm Guadalajara

    32/60

    Assembler Tutorial - 1996 Edition =2

    J@ F@T?*CT7F

    Purpose Conditional ,ump# and the state of the flags is taen into account!

    SyntaxJ! 6abe6

    t ,umps if the sign is on!The ,ump is done if @ E 1!

    '.* Instru!tions for !y!les: loo$

    They transfer the process flow# conditionally or unconditionally# to a destiny# repeating this

    action until the counter is &ero!

    (**" (**"E (**"&E

    ;77% F@T?*CT7F

    Purpose To generate a cycle in the program!

    Syntax

    (**" 6abe6

    The loop instruction decreases CH on 1# and transfers the flow of the program to the laelgien as operator if CH is different than 1!

    ;77%. F@T?*CT7F

    Purpose To generate a cycle in the program considering the state of Q!

    Syntax

    (**"E 6abe6

    This instruction decreases CH y 1! f CH is different to &ero and Q is e:ual to 1# then theflow of the program is transferred to the lael indicated as operator!

    ;77%F. F@T?*CT7F

    Purpose To generate a cycle in the program# considering the state of Q!

    Syntax

    (**"&E 6abe6

    This instruction decreases one from CH and transfers the flow of the program only if Q is

    different to A!

  • 8/9/2019 Asm Guadalajara

    33/60

    Assembler Tutorial - 1996 Edition ==

    '.; Countin& instru!tions

    They are used to decrease or increase the content of the counters!

    DEC$&C

    8.C F@T?*CT7F

    Purpose To decrease the operator!

    Syntax

    DEC destin?

    This operation sutracts 1 from the destiny operator and stores the new alue in the sameoperator!

    FC F@T?*CT7F

    Purpose To increase the operator!

    Syntax

    $&C destin?

    The instruction adds 1 to the destiny operator and eeps the result in the same destinyoperator!

    '.9 Com$arison instru!tions

    They are used to compare operators# and they affect the content of the flags!

    C,"C,"! C,"! C,"!

    C4% F@T?*CT7F

    Purpose To compare the operators!

    Syntax

    C," destin? sur

  • 8/9/2019 Asm Guadalajara

    34/60

    Assembler Tutorial - 1996 Edition =>

    C4%@ (C4%@3) (C4%@0) F@T?*CT7F

    Purpose To compare chains of a yte or a word!

    SyntaxC," destin? sur

  • 8/9/2019 Asm Guadalajara

    35/60

    Assembler Tutorial - 1996 Edition =5

    C4C F@T?*CT7F

    Purpose To complement the cartage flag!

    SyntaxC,C

    This instruction complements the state of the C flag# if C E A the instructions e:uals it to1# and if the instruction is 1 it e:uals it to A!

    0e could say that it only inerts the alue of the flag!

    @TC F@T?*CT7F

    Purpose To actiate the cartage flag!

    Syntax

    !.C

    This instruction puts the C flag in 1!

    @T8 F@T?*CT7F

    Purpose To actiate the address flag!

    Syntax

    !.D

    The @T8 instruction puts the 8 flag in 1!

    @T F@T?*CT7F

    Purpose To actiate the interruption flag!

    Syntax

    !.$

    The instruction actiates the flag# and this enales the maskarableeternal interruptions(the ones which only function when E 1)!

  • 8/9/2019 Asm Guadalajara

    36/60

    Assembler Tutorial - 1996 Edition =6

    ( Interru$tions and file mana&in&

    Table of Contents

    (.1 Internal ard/are interru$tions(." E,ternal ard/are interru$tions(.% Soft/are interru$tions(.' )ost Common interru$tions

    (.1 Internal ard/are interru$tions

    nternal interruptions are generated y certain eents which come during the eecution of aprogram!

    This type of interruptions are managed on their totality y the hardware and it is not possile tomodify them!

    ' clear eample of this type of interruptions is the one which actuali&es the counter of thecomputer internal cloc# the hardware maes the call to this interruption seeral times during asecond in order to maintain the time to date!

    .en though we cannot directly manage this interruption# since we cannot control the timedating y means of software# it is possile to use its effects on the computer to our enefit# foreample to create a irtual cloc dated continuously thans to the cloc/s internal counter! 0eonly hae to write a program which reads the actual alue of the counter and to translates it intoan understandale format for the user!

    (." E,ternal ard/are interru$tions

    .ternal interruptions are generated y peripheral deices# such as eyoards# printers#communication cards# etc! They are also generated y coprocessors! t is not possile todeactiate eternal interruptions!

    These interruptions are not sent directly to the C%*# ut rather they are sent to an integratedcircuit whose function is to eclusiely handle this type of interruptions! The circuit# called%C259'# is controlled y the C%* using for this control a series of communication wayscalled paths!

    (.% Soft/are interru$tions

    @oftware interruptions can e directly actiated y the assemler inoing the numer of thedesired interruption with the FT instruction!

    The use of interruptions helps us in the creation of programs# and y using them our programsare shorter# it is easier to understand them and they usually hae a etter performance mostlydue to their smaller si&e!

    This type of interruptions can e separated in two categories the operatie system 87@interruptions and the 37@ interruptions!

    The difference etween the two is that the operatie system interruptions are easier to use utthey are also slower since these interruptions mae use of the 37@ to achiee their goal# on theother hand the 37@ interruptions are much faster ut they hae the disadantage that since they

  • 8/9/2019 Asm Guadalajara

    37/60

    Assembler Tutorial - 1996 Edition =G

    are part of the hardware# they are ery specific and can ary depending een on the rand of themaer of the circuit!

    The election of the type of interruption to use will depend solely on the characteristics you wantto gie your program speed# using the 37@ ones# or portaility# using the ones from the 87@!

    (.' )ost !ommon interru$tions

    Table of Contents

    (.'.1 Int "13 =0S interru$tion>: )ulti$le !alls to 0S fun!tions.(.'." Int 1: ?ideo in$ut@out$ut.(.'.% Int 163 =#I0S interru$tion>: eyboard in$ut@out$ut.(.'.' Int 1*3 =#I0S interru$tion>: +rinter in$ut@out$ut.

    (.'.1 "13 Interru$tion

    Purpose To call on dierse 87@ functions!

    Syntax

    $nt 21G

    Fote 0hen we wor in T'@4 program is necessary to specify that the alue we are usingis headecimal!

    This interruption has seeral functions# to access each one of them it is necessary that the

    function numer which is re:uired at the moment of calling the interruption is in the '$register!

    unctions to display information to the ideo!

    02G .hiits output0G Chain mpression (ideo)40G 0riting in deicePfile

    unctions to read information from the eyoard!

    01G nput from the eyoard0AG nput from the eyoard using uffer3#G ?eading from deicePfile

    unctions to wor with files!

    n this section only the specific tas of each function is eposed# for a reference aoutthe concepts used# refer to unit G# titled ntroduction to file handling!

    FCB Method

    0#G 7pen file14G @e:uential reading15G @e:uential writing

    1%G Create file21G ?andom reading22G ?andom writing

  • 8/9/2019 Asm Guadalajara

    38/60

    Assembler Tutorial - 1996 Edition =

    Handles

    3CG Create file3DG 7pen file3EG Close file drier3#G ?eading from filePdeice

    40G 0riting in filePdeice42G 4oe pointer of readingPwriting in file

    A2$ *FCT7F

    Use t displays one character to the screen!

    Calling registers

    '$ E A2$8; E Oalue of the character to display!

    eturn registers

    Fone!

    This function displays the character whose headecimal code corresponds to the alue stored inthe 8; register# and no register is modified y using this command!

    The use of the >A$ function is recommended instead of this function!

    A9$ *FCT7F

    Use t displays a chain of characters on the screen!

    Call registers

    '$ E A9$8@8H E 'ddress of the eginning of a chain of characters!

    eturn registers

    Fone!

    This function displays the characters# one y one# from the indicated address in the 8@8Hregister until finding a R character# which is interpreted as the end of the chain!

    t is recommended to use the >A$ function instead of this one!

    >A$ *FCT7F

    Use To write to a deice or a file!

    Call registers

    AG = >A$X = %ath of communicationCX = Iuantity of ytes to writeD!:DX = 'ddress of the eginning of the data to write

    eturn registersC# = 0 if there was no mistae AX = Fumer of ytes written

    C# = 1 if there was a mistae AX = .rror code

  • 8/9/2019 Asm Guadalajara

    39/60

    Assembler Tutorial - 1996 Edition =9

    The use of this function to display information on the screen is done y giing the 3H registerthe alue of 1 which is the pre-assigned alue to the ideo y the operatie system 4@-87@!

    A1$ *FCT7F

    Use To read a eyoard character and to display it!

    Call registers

    '$ E A1$

    eturn registers

    '; E ?ead character

    t is ery easy to read a character from the eyoard with this function# the headecimal code ofthe read character is stored in the '; register! n case it is an etended register the '; registerwill contain the alue of A and it will e necessary to call on the function again to otain the

    code of that character!

    A'$ *FCT7F

    Use To read eyoard characters and store them on the uffer!

    Call registers

    AG = 0AGD!:DX = 'rea of storage address.E 0 = Iuantity of ytes in the area.E 1 = Iuantity of ytes read

    from .E 2 till.E 0 K 2 = read characters

    eturn characters

    Fone!

    The characters are read and stored in a predefined space on memory! The structure of this spaceindicate that in the first yte are indicated how many characters will e read! 7n the second ytethe numer of characters already read are stored# and from the third yte on the read charactersare written!

    0hen all the indicated characters hae een stored the speaer sounds and any additionalcharacter is ignored! To end the capture of the chain it is necessary to hit K.FT.?L!

    =$ *FCT7F

    Use To read information from a deice or file!

    Call registers

    AG = 3#GX = Fumer assigned to the deiceCX = Fumer of ytes to processD!:DX = 'ddress of the storage area

    eturn registersC E A if there is no error and 'H E numer of read ytes!C E 1 if there is an error and 'H will contain the error code!

  • 8/9/2019 Asm Guadalajara

    40/60

    Assembler Tutorial - 1996 Edition >A

    A$ *FCT7F

    Use To open an C3 file

    Call registers'$ E A$8@8H E %ointer to an C3

    eturn registers

    '; E AA$ if there was no prolem# otherwise it returns to A$

    1>$ *FCT7F

    Use To se:uentially read an C3 file!

    Call registers

    '$ E 1>$8@8H E %ointer to an C3 already opened!

    eturn registers

    '; E A if there were no errors# otherwise the corresponding error code will e returned 1 errorat the end of the file# 2 error on the C3 structure and = partial reading error!

    0hat this function does is that it reads the net loc of information from the address gien y8@8H# and dates this register!

    15$ *FCT7F

    Use To se:uentially write and C3 file!

    Call registers

    '$ E 15$8@8H E %ointer to an C3 already opened!

    eturn registers

    '; E AA$ if there were no errors# otherwise it will contain the error code 1 full dis or read-only file# 2 error on the formation or on the specification of the C3!

    The 15$ function dates the C3 after writing the register to the present loc!

    16$ *FCT7F

    Use To create an C3 file!

    Call registers

    '$ E 16$8@8H E %ointer to an already opened C3!

    eturn registers'; E AA$ if there were no errors# otherwise it will contain the A$ alue!t is ased on the information which comes on an C3 to create a file on a dis!

  • 8/9/2019 Asm Guadalajara

    41/60

    Assembler Tutorial - 1996 Edition >1

    21$ *FCT7F

    Use To read in an random manner an C3 file!

    Call registers'$ E 21$8@8H E %ointer to and opened C3!

    eturn registers

    ' E AA$ if there was no error# otherwise '$ will contain the code of the error 1 if it is the endof file# 2 if there is an C3 specification error and = if a partial register was read or the file

    pointer is at the end of the same!

    This function reads the specified register y the fields of the actual loc and register of anopened C3 and places the information on the 8T'# 8is Transfer 'rea!

    22$ *FCT7F

    Use To write in an random manner an C3 file!

    Call registers

    '$ E 22$8@8H E %ointer to an opened C3!

    eturn registers'; E AA$ if there was no error# otherwise it will contain the error code 1 if the dis is full or

    the file is an only read and 2 if there is an error on the C3 specification!

    t writes the register specified y the fields of the actual loc and register of an opened C3! twrites this information from the content of the 8T'!

    =C$ *FCT7F

    Use To create a file if it does not eist or leae it on A length if it eists# $andle!

    Call registers

    '$ E =C$C$ E ile attriute8@8H E %ointer to an '@C specification!

    eturn registers

    C E A and 'H the assigned numer to handle if there is no error# in case there is# C will e 1and 'H will contain the error code = path not found# > there are no handles aailale to assignand 5 access denied!

    This function sustitutes the 16$ function! The name of the file is specified on an '@C chain#which has as a characteristic eing a conentional chain of ytes ended with a A character!

    The file created will contain the attriutes defined on the CH register in the following manner

  • 8/9/2019 Asm Guadalajara

    42/60

    Assembler Tutorial - 1996 Edition >2

    'a6ue Attributes

    00G &ra602G Gidden04G !?ste

    0%G Gidden and s?ste

    The file is created with the reading and writing permissions! t is not possile to createdirectories using this function!

    =8$ *FCT7F

    Use t opens a file and returns a handle!

    Call registers

    '$ E =8$'; E manner of access8@8H E %ointer to an '@C specification

    eturn registers

    C E A and 'H E handle numer if there are no errors# otherwise C E 1 and 'H E error codeA1$ if the function is not alid# A2$ if the file was not found# A=$ if the path was not found#A>$ if there are no aailale handles# A5$ in case access is denied# and AC$ if the access codeis not alid!

    The returned handled is 16 its!

    The access code is specified in the following way

    =.$ *FCT7F

    Use Close file (handle)!

    Call registers

    '$ E =.$3H E 'ssigned handle

    eturn registers

    C E A if there were no mistaes# otherwise C will e 1 and 'H will contain the error codeA6$ if the handle is inalid!

    This function dates the file and frees the handle it was using!

    =$ *FCT7F

    Use To read a specific :uantity of ytes from an open file and store them on a specific uffer!

    BITS 7 6 5 4 3 2 1

    0 0 0 *n6? reading 0 0 1 *n6? riting 0 1 0 eading/riting E!E'ED

  • 8/9/2019 Asm Guadalajara

    43/60

    Assembler Tutorial - 1996 Edition >=

    (.'." 1!This means from the left per computer screen corner to right lower computer screen! Thereforethe numeric alues that the 8$ and 8; registers get in tet model are from A to 2> for rows andfrom A to G9 for columns!

    A9$ *FCT7F

    Use @hows a defined character seeral times on the computer screen with a defined attriute#starting with the actual cursor position!

    Call registers

    AG = A9$A( = Character to displayG = Oideo page# where the character will display it

    ( = 'ttriute to usenumer of repetition!

  • 8/9/2019 Asm Guadalajara

    44/60

    Assembler Tutorial - 1996 Edition >>

    eturn registers

    Fone

    This function displays a character on the computer screen seeral times# using a specified

    numer in the CH register ut without changing the cursor position on the computer screen!

    A'$ *FCT7F

    Use 8isplays a character in the actual cursor position!

    Call registers

    AG = 0AGA( = Character to displayG = Oideo page where the character will display it( = Color to use (graphic mode only)!

    CX = numer of repetitions

    eturn registers

    Fone!

    The main difference etween this function and the last one is that this one doesn/t allowmodifications on the attriutes neither does it change the cursor position!

    A.$ *FCT7F

    Use 8isplays a character on the computer screen dates the cursor position!

    Call registersAG =A.$A( =Character to displayG =Oideo page where the character will display it( =Color to use (graphic mode only)!

    eturn registers

    Fone

    (.'.% 163 interru$tion

    0e will see two functions of the 16 h interruption# these functions are called y using the '$register!

    unctions of the 16h interruption

    00G unction# reads a character from the eyoard!01G unction# reads the eyoard state!

    AA$ *FCT7F

    Use ?eads a character from the eyoard!

    Call registers

    '$ E AA$

  • 8/9/2019 Asm Guadalajara

    45/60

    Assembler Tutorial - 1996 Edition >5

    eturn registers

    AG = @can code of the eyoardA( = '@C alue of the character

    0hen we use this interruption# the program eecuting is halted until a character is typed# if thisis an '@C alue it is stored in the 'h register# .lse the scan code is stored in the '; registerand the '$ register contents the alue AAh!

    The proposal of the scan code is to use it with the eys without '@C representation as K';TLKC7FT?7;L# the function eys and so on!

    A1$ *FCT7F

    Use ?eads the eyoard state

    Call registers

    AG = 01G

    eturn registers

    f the flag register is &ero# this means# there is information on the uffer memory# else# there isno information in the uffer memory! Therefore the alue of the 'h register will e the alueey stored in the uffer memory!

    (.'.' 1*3 Interru$tion

    Purpose $andles the printer inputPoutput!

    Syntax

    $nt 17G

    This interruption is used to write characters on the printer# sets printer and reads the printerstate!

    unctions of the 16h interruptions

    00G unction# prints alue '@C out01G unction# sets printer02G unction# the printer state

    AA$ *FCT7F

    Use 0rites a character on the printer!

    Call registers

    AG = AA$A( = Character to print!DX = %ort to use!

    eturn registers

    AG = %rinter deice state!

  • 8/9/2019 Asm Guadalajara

    46/60

    Assembler Tutorial - 1996 Edition >6

    The port to use is in the 8H register# the different alues are ;%T1 E A# ;%T2 E 1# ;%T= E 2 !!!The printer deice state is coded it y it as follows

    $. 1/0 ,EA&$&I

    0 1 .e aited tie is er1 -2 -3 1 in8ut/ut8ut errr4 1 Csen 8rinter

    5 1 ut--8a8er% 1

  • 8/9/2019 Asm Guadalajara

    47/60

    Assembler Tutorial - 1996 Edition >G

    %ort to use is defined in the 8H register# for eample ;%TEA# ;%T2E1# and so onThe state of the printer is coded it y it as follows

    $. 1/0 ,EA&$&I

    0 1 .e aited tie is er1 -2 -3 1 in8ut/ut8ut errr4 1 Csen 8rinter

    5 1 ut--8a8er% 1

  • 8/9/2019 Asm Guadalajara

    48/60

  • 8/9/2019 Asm Guadalajara

    49/60

    Assembler Tutorial - 1996 Edition >9

    (.6.' SeBuential /ritin&

    3efore we can perform writing to the dis it is necessary to define the data transfer area usingfor this end the 1'$ function of the 21$ interruption!

    The 1'$ function does not return any state of the dis nor or the operation# ut the 15$function# which is the one we will use to write to the dis# does it on the '; register# if this oneis e:ual to &ero there was no error and the fields of the current register and loc are dated!

    (.6.( SeBuential readin&

    3efore anything we must define the file transfer area or 8T'! n order to se:uentially read weuse the 1>$ function of the 21$ interruption! The register to e read is the one which is defined

    y the current loc and register! The '; register returns to the state of the operation# if ';contains a alue of 1 or = it means we hae reached the end of the file! ' alue of 2 means that

    the C3 is wrongly structured!n case there is no error# '; will contain the alue of A and the fields of the current loc andregister are dated!

    (.6.6 5andom readin& and /ritin&

    The 21$ function and the 22$ function of the 21$ interruption are the ones in charge ofreali&ing the random readings and writings respectiely!

    The random register numer and the current loc are used to calculate the relatie position ofthe register to read or write!

    The '; register returns the same information for the se:uential reading of writing! Theinformation to e read will e returned on the transfer area of the dis# liewise the informationto e written resides on the 8T'!

    (.6.* Closin& a file

    To close a file we use the 1A$ function of the 21$ interruption!

    f after inoing this function# the '; register contains the $ alue# this means that the filehas changed position# the dis was changed or there is error of dis access!

    (.* Cannels of !ommuni!ation

    Table of Contents

    (.*.1 orin& /it andles(.*." 4un!tions to use andles

  • 8/9/2019 Asm Guadalajara

    50/60

    Assembler Tutorial - 1996 Edition 5A

    (.*.1 orin& /it andles

    The use of handles to manage files greatly facilitates the creation of files and programmer canconcentrate on other aspects of the programming without worrying on details which can e

    handled y the operatie system!

    The easy use of the handles consists in that to operate o a file# it is only necessary to define thename of the same and the numer of the handle to use# all the rest of the information isinternally handled y the 87@!

    0hen we use this method to wor with files# there is no distinction etween se:uential orrandom accesses# the file is simply taen as a chain of ytes!

    (.*." 4un!tions to use andles

    The functions used for the handling of files through handles are descried in unit 6

    nterruptions# in the section dedicated to the 21$ interruption!

    6 )a!ros and $ro!edures

    Table of Contents

    6.1 +ro!edures6." )a!ros

    6.1 +ro!edureE4I2ITI0204+50CEU5E

    ' procedure is a collection of instructions to which we can direct the flow of our program# andonce the eecution of these instructions is oer control is gien ac to the net line to processof the code which called on the procedure!

    %rocedures help us to create legile and easy to modify programs!

    't the time of inoing a procedure the address of the net instruction of the program is ept onthe stac so that# once the flow of the program has een transferred and the procedure is done#one can return to the net line of the original program# the one which called the procedure!

    S2TAD04A+50CEU5E

    There are two types of procedures# the intrasegments# which are found on the same segment ofinstructions# and the inter-segments which can e stored on different memory segments!

    0hen the intrasegmentprocedures are used# the alue of % is stored on the stac and when theintrasegmentsare used the alue of C@% is stored!To diert the flow of a procedure (calling it)# the following directie is used

    CALL NamO!T"P#$%&'#

    The part which mae a procedure are

    8eclaration of the procedure

    Code of the procedure

    ?eturn directie

    Termination of the procedure

  • 8/9/2019 Asm Guadalajara

    51/60

    Assembler Tutorial - 1996 Edition 51

    or eample# if we want a routine which adds two ytes stored in '$ and '; each one# andeep the addition in the 3H register

    Adding "r< &ear ; De

  • 8/9/2019 Asm Guadalajara

    52/60

    Assembler Tutorial - 1996 Edition 52

    6."." Synta, of a )a!ro

    The parts which mae a macro are

    8eclaration of the macro

    Code of the macro

    4acro termination directie

    The declaration of the macro is done the following way

    Nam(a%#$ (ACRO )*a#am+#1, *a#am+#2-

    .en though we hae the functionality of the parameters it is possile to create a macro whichdoes not need them!The directie for the termination of the macro is .F84

    'n eample of a macro# to place the cursor on a determined position on the screen is

    "sitin ,AC* C6un "H!G AX "H!G X "H!G DX ,*' AG 02G ,*' DG ,*' D( C6un ,*' G 0 $&. 10G "*" DX "*" X "*" AX

    E&D,

    To use a macro it is only necessary to call it y its name# as if it were another assemlerinstruction# since directies are no longer necessary as in the case of the procedures! .ample

    "sitin + %

    6.".% )a!ro 7ibraries

    7ne of the facilities that the use of macros offers is the creation of liraries# which are groups ofmacros which can e included in a program from a different file!

    The creation of these liraries is ery simple# we only hae to write a file with all the macroswhich will e needed and sae it as a tet file!

    To call these macros it is only necessary to use the following instruction ncludeFame7fTheile# on the part of our program where we would normally write the macros# this is#at the eginning of our program# efore the declaration of the memory model!

    The macros file was saed with the name of 4'C?7@!THT# the instruction nclude would eused the following way

  • 8/9/2019 Asm Guadalajara

    53/60

    Assembler Tutorial - 1996 Edition 5=

    ;eginning te 8rgra$n

  • 8/9/2019 Asm Guadalajara

    54/60

    Assembler Tutorial - 1996 Edition 5>

    Fourth example

    -a10027D:0100 ,*' AG01 ;#un

  • 8/9/2019 Asm Guadalajara

    55/60

    Assembler Tutorial - 1996 Edition 55

    Seventh example

    -a10027D:0100 ,*' AG02 ; #un

  • 8/9/2019 Asm Guadalajara

    56/60

    Assembler Tutorial - 1996 Edition 56

    Tenth example

    -a10027D:0100 ,*' AG01 ; #un

  • 8/9/2019 Asm Guadalajara

    57/60

    Assembler Tutorial - 1996 Edition 5G

    ))05E05EAASSE)#7E5SSE)#7E5+50G5A)S+50G5A)S EDA)+7ESEDA)+7ES==USI2GUSI2GTAS)TAS) +50G5A)+50G5A)>>

    ;nae te 8rgra:neas

    ;de6 sa66sta

  • 8/9/2019 Asm Guadalajara

    58/60

    Assembler Tutorial - 1996 Edition 5

    ;nae te 8rgra:treeasde6 sa66!.AC@

  • 8/9/2019 Asm Guadalajara

    59/60

    Assembler Tutorial - 1996 Edition 59

    $.ERCGA "*C "H!G AX ;8uses te a6ue te register AX in te sta

  • 8/9/2019 Asm Guadalajara

    60/60

    Assembler Tutorial - 1996 Edition 6A

    de6 sa66; te nae te 8rgra is seenassta