ASMLABMANUAL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 95

Experiment # 1 Introduction to Assembly Language Programming

Introduction: The aim of this experiment is to introduce the student to assembly language programming, and the use of the tools that he will need throughout the lab experiments. This first experiment let the student use the DOS Debugger and the Microsoft Macro Assembler (MASM . MASM related tools are introduced! these include the "rogrammer#s $or%&ench ("$& and 'ode(iew ('( . Such tools are interacti)e means for writing lin%ing, and debugging assembly language programs. Objectives:

*+ ,ntroduction to the Microsoft Macro Assembler (MASM


-+ .eneral structure of an assembly language program /+ 0se of the Dos Debugger program 1+ 0se of the "$&, and 'ode (iew ('( . 2+ ,ntroducing Data representation in assembly Overview: ,n general, programming of a microprocessor usually ta%es se)eral iterations before the right se3uence of machine code instructions is written. The process, howe)er, is facilitated using a special program called an 4Assembler5. The Assembler allows the user to write alphanumeric instructions, or mnemonics, called Assembly 6anguage instructions. The Assembler, in turn, generates the desired machine instructions from the Assembly 6anguage instructions. Assembly 6anguage programming consists of the following steps7 STEP PRODUCES 8diting Source 9ile Assembling Ob:ect 9ile 6in%ing 8xecutable 9ile 8xecuting ;esults Table ! : Asse"bl# $anguage Progra""ing P%ases

* / 1

Asse"bling t%e &rogra": The assembler is used to con)ert the assembly language instructions to machine code. ,t is used immediately after writing the Assembly 6anguage program. The assembler starts by chec%ing the syntax, or )alidity of the structure, of each instruction in the source file. ,f any errors are found, the assembler displays a report on these errors along with a brief explanation of their nature. <owe)er, if the program does not contain any errors, the assembler produces an ob:ect file that has the same name as the original file but with the 4ob:5 extension.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page

o) *+

$in,ing t%e &rogra"7 The lin%er is used to con)ert the ob:ect file to an executable file. The executable file is the final set of machine code instructions that can directly be executed by the microprocessor. ,t is different than the ob:ect file in the sense that it is self+contained and re+locatable. An ob:ect file may represent one segment of a long program. This segment can not operate by itself, and must be integrated with other ob:ect files representing the rest of the program, in order to produce the final self+contained executable file. ,n addition to the executable file, the lin%er can also generate a special file called the 4map5 file. This file contains information about the start, end, and the length of the stac%, code, and data segments. ,t also lists the entry point of the program. E-ecuting t%e &rogra" The executable file contains the machine language code. ,t can be loaded in the ;AM and be executed by the microprocessor simply by typing, from the DOS prompt, the name of the file followed by the 'arriage ;eturn =ey (8nter =ey . ,f the program produces an output on the screen, or a se3uence of control signals to control a piece of hardware, the effect should be noticed almost immediately. <owe)er, if the program manipulates data in memory, nothing would seem to ha)e happened as a result of executing the program. Debugging t%e &rogra" The Debugger can also be used to find logical errors in the program. 8)en if a program does not contain syntax errors it may not produce the desired results after execution. 6ogical errors may be found by tracing the action of the program. Once found, the source file should be reedited to fix the problem, then re+ assembled and re+lin%ed. A special program called the 4Debugger5 is designed for that purpose. The debugger allows the user to trace the action of the program, by single stepping through the program or executing the program up to a desired point, called break point. ,t also allows the user to inspect or change the contents of the microprocessor internal registers or the contents of any memory location.

T%e DOS.Debugger: The DOS 4Debug5 program is an example of a simple debugger that comes with MS+DOS. <ence, it is a)ailable on any "'. ,t was initially designed to gi)e the user the capability to trace logical errors in executable files. ,t allows the user to ta%e an existing executable file and unassembled it, i.e. con)ert it to assembly language. Also, it allows the user to write assembly language instructions directly, and then con)ert them to machine language. The program is simple and easy to use, but offers limited capabilities, which ma%e it unsuitable for serious Assembly language programming.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page / o) *+

&elow, are summari>ed the basic DOS+Debugger commands. 'OMMA?D Assemble 'ompare Dump 8nter 9ill .o <ex ,nput 6oad Mo)e ?ame Output "roceed Euit ;egister Search Trace 0nassemble $rite MS.MASM7 Microsoft#s Macro Assembler (MASM is an integrated software pac%age written by Microsoft 'orporation for professional software de)elopers. ,t consists of an editor, an assembler, a lin%er and a debugger ('ode(iew . The programmer#s wor%bench combines these four parts into a user+friendly programming en)ironment with built+in on+line help. The following are the steps used if you are to run MASM from DOS. COMMA0D 1I$E 0AME Edit, any editor will do ?ame.asm Mas" 9ilename ?ame.ob: $in, 9ilename ?ame.exe 9ilename ?ote7 Steps - and / may be done in one single ?ame.asm and command7 M$ )ilena"e!as" ?ame.ob: Table !2: Asse"bl# $anguage Progra""ing P%ases S@?TAA A BaddressC ' range address D BrangeC 8 address BlistC 9 range list . BDaddressC BaddressesC < )alue* )alue, port 6 BaddressC Bdri)eC Bfirst sectorC BnumberC M range address ? BpathnameC Bargument listC O port byte " BDaddressC BnumberC E ; BregisterC S range list T BDaddressC B)alueC 0 BrangeC $ BaddressC Bdri)eC Bfirst sectorC BnumberC Table !/7 'ommon DOS+Debug commands

/ 2 3

MS.P45: The "$& allows the user to define a pro:ect that may contain one or more files. Then, the user may select and sa)e all the necessary assembling, lin%ing, and debugging options for that pro:ect. Once these options are set, the user need not set them again for that pro:ect. The "$& allows the user to edit, assemble, run, or debug his program without lea)ing the "$& en)ironment. ,t also allows the user to get help on any %eyword by pointing to the %eyword and pressing the 9* %ey. 0otes on t%e use o) MS.MASM: MASM may be run under DOS en)ironment, or through "$&.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2 o) *+

Running MASM and Code6iew Debugger )ro" t%e MSDOS &ro"&t: ,f you donFt li%e the integrated "$&, you might run :ust the necessary programs from the MSDOS prompt. <ere are the steps, assuming that your program is called Gpro:G7 *. -. Open a DOS window. Set the "AT< so that the MASM programs are a)ailable

The MASM programs are on the 8 dri)e! set the path so that DOS can find them. This only needs to be done once each time you open a MSDOS prompt. set "AT<DH"AT<H!87ImasmJ**Ibin! 87ImasmJ**Ibinr /. 0se a Text 8ditor to 8dit the .ASM 9ile

'reate your file using one of the following programs7 notepad pro:.asm wordpad pro:.asm edit pro:.asm Ma%e sure it has a .ASM ending. Also, be sure you are doing your wor% on the A7 dri)e, or on the D7I$O;=A;8A, and not in the '7I$,?DO$S directory, or other dri)es. 1. ;un the 'ompilerK6in%er to generate an .8A8 file from the .ASM file ml K9l KLi pro:.asm

Ma%e sure you use the two switches7 K9l creates a listing (.6ST file (the letter to the right of the 9 is a lower+case 6 ! KLi ma%es the finished product compatible with the 'ode(iew debugger. @ouFll be notified of any errors with your program, and error statements should be placed in your .6ST file (pro:.lst in this case . 2. ;un the "rogram @our final program (if there were no errors in the pre)ious step will ha)e a .8A8 ending. To :ust run it, type7 pro: ,f you want to use the 'ode(iew debugger to examine the instructions, registers, etc.7 c) pro: This brings up the regular full+screen )ersion of 'ode(iew. Running MASM and Code6iew P45: The basic steps for creating, building, and debugging an assembly language program are gi)en below. The examples gi)en in this first session are )ery simple! their main purpose is to show how o uses the "$& en)ironment. A pro:ect is a complete set of files needed to define and build an assembly language program. The source language files for your programs are, of course, part of this pro:ect, but also there are listing files, and symbol table files for debugging, and files gi)ing directions for building your program. ,t is best to create a separate folder to contain the pro:ect files for each programming assignment. @ou should copy this folder on a floppy dis% for later use.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3 o) *+

'reate a folder for your new pro:ect. 9or the first assignment, the folder name could be lab*. 'opy any files that are needed from your floppy dis% into your folder. ;un "$&.8A8 by double clic%ing on the file or its shortcut in the '7IMASMJ**I&,? folder. The shortcut may be copied and the copy mo)ed to the des%top to ma%e it easier to find the "$&. Once the "$& is running (and the DOS window appears , you may create a new pro:ect using the pro:ect menu. Type the correct path to the new pro:ect folder with pro:ect name and press the set &roject te"&late button. Select ;untime support7 0one, "ro:ect Templates7 DOS:E7E, and press O8! @ou donFt ha)e any program files to add to the pro:ect yet, so clic% save list when the add file window appears. 0nder the Options menu, select build o&tions. Select Use Debug O&tions! Select O8. 0sing the O&tions "enu:language o&tions:MASM9 select Set Debug O&tions! 'hec% the boxes that will create a listing file with source, machine language, and symbol table7 :enerate $isting 1ile9 $ist :enerated Instructions9 Include all Source $ines9 Code6iew Debugger! Press O8! 0sing the Options "enu:lin, options, select Code6iew debugger9 debug o&tions. "ress the additional debug options GbuttonG and select 1ull map output. This will gi)e you a lin% map that tells where each program component will be loaded. Select O8! Select new under the file menu to get an editor window for your program. 8nter the program. Sa)e your program naming it with a .ASM suffix, e.g. 7 "**.ASM Select edit &roject from the pro:ect menu, and add your new assembly program into the pro:ect list. Save t%e list. Start the debugger, running your program, by selecting debug from the ;un menu. ,t will as% you to build your pro:ect. That is a good idea, select Rebuild All. The Assembler will assemble your code, the lin%er will be called, and if there are no errors, the debugger window will appear. @ou can then start debugging. ,f there were any errors, select 6iew Results, then clic% ne-t error under the pro:ect menu. ,t will show you the line containing the error in your source program, and you can correct it at this point. 'ontinue as%ing to see the next error until you run out of errors. Select debug from the pro:ect window to ha)e another try. ;epeat this procedure until you finally get the debugger. ?ow you can run your program. @ou may step through your program one line at a time by using 1; and 1 <, or you may select go (1+ and run it without interruption. ,n order to watch the contents of memory change, you need to select the memory window options in the options menu (of the debugger . 'hec% the box which re3uests that the memory window be continually re+ e)aluated.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page + o) *+

,f you donFt chec% this box, the memory window will continue to reflect memory contents :ust before your program started running. &rea%points may be inserted at any line of code using the set brea,&oint item in the Data menu, or press 1*. ,f go (or 1+ is selected, the program will stop on any line encountered with a brea%point that is set. Selecting source window options from the window menu allows you to ha)e mixed source lines and ob:ect code. This will allow you to compare the instructions that are actually running with the lines of source code that you pro)ided. 1 < will single step through your code, but will not single step through any called subroutines (,t will Gstep+o)erG the subroutine call. The subroutine will be called correctly! it will simply not be debugged. This is handy when you are using a subroutine that has already been debugged, or when a system routine is called. 1; will single Gstep+intoG a subroutine that is called, allowing you to debug the subroutine itself. @ou may execute down to a gi)en instruction by right clic%ing on the source line, mo)ing the cursor to the source line, and then pressing the 1= %ey. Ta%ing the time to add comments to your code while first entering it will sa)e much time in the long run. ,t is )ery hard to figure out what an undocumented assembly language program is doing after e)en a brief intermission. 'omments help you to 3uic%ly locate desired sections of code. 0ote: On the machines at the lab, it is fre3uently necessary to ma%e sure the path is set correctly for the "$& to run. ;ight mouse+clic% on the "$& shortcut icon. Select Pro&erties, and then select the Progra" tab. 0nder 5atc% )ile, type C:>MASM? 6ARS. ,n the lab, only the networ% administrator can do this procedure. Pre $ab 4or,7 ,n this experiment you will practice editing, assembling, and lin%ing an Assembly language program. 9irst you will type a short program, using DOS 8dit, then assemble and lin% the program using the MASM Assembler and 6in%er. Second you will practice the DOS+Debugger, and compare it to MASM#s 'ode(iew. 9inally, you will use MS+"$& to de)elop a pro:ect around your short program and practice some of the ad)antages of "$&. *+ Study the material gi)en in part , of this manual. -+ ;e)iew the material related to data representation. /+ $rite the attached programs and bring them on a floppy dis% to the lab. 0se the DOS editor or the $indows notepad. ,f you use a word processor, ma%e sure that you chose the option Save As Text while sa)ing 0ote: T%e E-it )unction7 The following instructions terminate the program and exit to DOS. MO( A<, 1'< ,?T -*< $ab 4or,: *+ Assemble, 6in% and ;un program *. -+ 0se the Debugger to run your program. >5I0>0E4.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ? o) *+

/+ 1+ J+ 2+ J+ N+ O+

?otice the )alues gi)en by the assembler to the numbers you used in your program. Draw a table and write each )alue with its corresponding representation. $hat do you concludeM ;epeat the same procedure using "$& and 'ode(iew. $hat do you concludeM Dress a table and write the )alues assigned by the assembler to the )alues you wrote in the editor. 8xplain that. Assemble, 6in% and ;un program -. $hat )alue do you find in DA registerM ;epeat the same procedure using "$& and 'ode(iew. 'hange the line7 M06T* 8E0 -2 &y7 M06T* 8E0 -2<,

and run the program again. $hat do you noticeM P+ 'an you explain what the program doesM ! The following lines are :ust comments, they may be omitted, ! <owe)er, they are )ery useful. ! 6ab 8xp. Q * "rogram Q * ! Student ?ame7 TIT$E @A si"&le &rogra"A !MODE$ SMA$$ !STAC8 2/ !CODE MO6 A79 /<<< MO6 579 /<<<B MO6 C79 < << <5 MO6 D79 .3+?= MO6 A$9 CAC MO6 AB9 CaC MO6 A79 3C<<B I0T / B E0D Student ,D7 Section7

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page = o) *+

! 6ab 8xp. Q * "rogram Q ! Student ?ame7 TIT$E DOur second &rogra"D !MODE$ SMA$$ !STAC8 2/ !DATA MU$T EEU MU$T/ D4 !CODE MO6 A79 FDATA MO6 DS9 A7 MO6 A79 << MO6 579 MU$T MO6 C79 MU$T/ MU$T: ADD A79 57 DEC C7 G0H MU$T MO6 D79 A7 MO6 A79 3C<<B I0T / B E0D

Student ,D7

Section7

/+ +

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ; o) *+

E-&eri"ent I/ In&ut and Out&ut


Introduction: ,n this experiment you will be introduced to the basic ,nput and Output (,KO operations using assembly language. @ou will use the DOS interrupt (,?T -*< function calls to access the %eyboard and )ideo display. More details will also be gi)en on the structure of an assembly language program. The following ma:or points are discussed7 - (ariable declaration using7 D&, D$, DD - 'onstant declaration using7 8E0 - O99S8T operator - ,?T -*< with the functions *, -, O and P. Objectives: *+ Demonstrate %eyboard access using the DOS ,?T -*< function calls R*, R- and RO. -+ Demonstrate string display using the DOS ,?T -*< function call RP. /+ Show the difference between %eyboard read functions, with echo and without echo. I(O DOS 1unction Calls: Table -. *, summari>es the main ,KO functions. These functions are mainly used to read a character or a string from the %eyboard, which could be an input data to a program, and display characters or strings, which could be results, or an output, of a program7 1unction R*< R-<, RJ< RO< RP< RA< In&ut in A< A<, 'haracter in D6 A< A< A< E))ect ;ead a character with echo on the screen. Display a character on the screen. ?ote7 ,nterrupted by 'trl S &rea% A6 ;ead character without echo. ?o output Display a string terminated by a TU# sign Offset in DA ;ead a string of characters from the %eyboard Table /! 7 Simple ,KO DOS function calls Out&ut in A6 ?o output

DOS Dis&la# 1unctions: These are DOS functions R- and RJ for a single character display, and RP for a string display. DOS 1unctions </ and <?: &oth functions are identical, except that function R- can be interrupted by a control brea% ('trl+&rea% , while function RJ cannot. To display a single AS',, character at the current cursor position use the following se3uence of instructions7 MO( A<, RJ< !(Or7 MO( A<, R-< MO( D6, 'haracter 'ode ,?T -*< The 'haracter 'ode may be the ASCII code of the character ta%en from the AS',, table (See 8xperiment 1 Table 1.* or the character itself written between 3uotes. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page * o) *+

The following displays number - using its AS',, code7 MO( A<, RJ< MO( D6, /-< ,?T -*< This code also displays -7 MO( A<, RJ< MO( D6, T-# ,?T -*< DOS 1unctions <*: This function is used to display a string of characters ended with a TU# sign. The following code displays the string M8SSA.8 defined as7 M8SSA.8 D& TThis is the Message to be displayed#, TU# .'OD8 MO( DA, O99S8T M8SSA.8 MO( A<, RP< ,?T -*< DOS In&ut )unctions: These include reading a single character, with or without echo, functions R* and RO, and reading a whole string. 1unction < B and <;B I0T / B: To read single character and ha)e it echoed (displayed on the screen, use the following code7 MO( A<, R*< ,?T -*< !A6 contains now the AS',, code of the character read from the !%eyboard. ,f the character is to be read without echo, such as reading a password, use the following code7 MO( A<, RO< ,?T -*< !A6 contains now the AS',, code of the character read Reading a String: ;eading a string is accomplished by 9unction RA< ,?T -*<. DOS function RA< will accept a string of text entered at the %eyboard and copy that string into a memory buffer. DOS RA< is in)o%ed with DS7DA pointing to an input buffer, whose si>e should be at least three bytes longer than the largest input string anticipated.

&efore in)o%ing DOS function RA<, you must set the first byte of the buffer with the number of character spaces in the buffer. After returning from DOS function RA<, the second byte of the buffer will contain a )alue gi)ing the number of characters actually read form the %eyboard (Table /!/ . Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page < o) *+

&uffer 6ength

Actual 6ength 1igure /! 7 =eyboard buffer structure

9unction RA< 8ntry 8xit

;ead from =eyboard A< D RA< ! DA D address of %eyboard input buffer 9irst byte of buffer contains the si>e of the buffer (up to -22 Second byte of buffer contains the number of characters read. ;eading operation continues until buffer full, or a carriage return ('; D RD< is typed. Table /! : 7 9unctions RA< of DOS interrupt.

E-a"&le7 &elow is an example on the use of function RA<, when the user enters the word 4hello5. ,nput7 RO AA AA AA AA AA AA AA AA AA

MO( A<, RA< ,?T -*< !;ead from %eyboard the word 4hello5 Output7 RO R2 JO J2 J' J' J9 RD AA AA

E"&t# String7 Pre $ab 4or,7 ;e)iew the material gi)en in experiment number * for the use of "$&, and 'ode(iew. *. -. Study the attached hand out. ;e)iew the material related to data representation. $rite the attached programs and bring them on a floppy disc to the lab. 0se the "$& or DOS editor, or e)en the $indows notepad, to write your programs.

/.

0ote7 .i)e meaningful names to your programs, so that you can differentiate between them easily, e.g. pgm21.asm, stands for experiment - program *. $ab 4or,: *+ Assemble and 6in% program *. -+ Type the program#s name at the prompt to run the program. /+ $hat does the program doM ?otice how the program handles the three different characters. Assemble, 6in% and ;un program 1+ ;eplace the line7 MO( DA, O99S8T M8SSA.8 by7 68A DA, M8SSA.8 Then repeat step 1, what do you noticeM 'hec% with 'ode(iew the effects of the instruction 68A and the O99S8T operatorM Assemble, 6in% and ;un program / After running the program, notice here the effect of the characters RD< and RA< at the end of the line containing7 M8SSA.8. $hat is your conclusionM ?otice also the effects of the function calls R*<, RO<. Page o) *+

2+ J+ N+ O+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

$rite down all your conclusions. $ab Assign"ent: $rite an assembly language program that prompts you to enter a password of / characters in length. The password should not be echoed to the screen. The program then displays your name and ,D number on the screen. Submit your wor% at the end of the lab. ! Student ?ame7 Student ,D7 Section7

Title @E-&! / Progra" A ! This program displays the characters A & ', using ,?T -*< function R-. .MOD86 SMA66 .DATA A 8E0 #&# @ D& 1/< .STA'= -RR .'OD8 MO( MO( MO( AA, VDATA DS, AA A<, R! 6OAD 90?'T,O? R! 6OAD '<A;A'T8; TO &8 D,S"6A@8D ! 'A66 ,?T8;;0"T -*< ! 6OAD '<A;A'T8; TO &8 D,S"6A@8D ! 'A66 ,?T8;;0"T -*< ! 6OAD '<A;A'T8; TO &8 D,S"6A@8D ! 'A66 ,?T8;;0"T -*< ! 8xit to DOS

MO( D6, #A# ,?T -*< MO( D6, A ,?T -*< MO( D6, @ ,?T -*< MO( AA, 1'RR< ,?T -*<

8?D WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW Title @E-&! / Progra" /A ! This program displays a string terminated by a U sign using ,?T -*< function RP<. .MOD86 SMA66 .DATA M8SSA.8 D& TThis is the message to be displayed#, #U# .STA'= -RR .'OD8 Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page / o) *+

MO( MO(

AA, VDATA DS, AA

MO( DA, O99S8T M8SSA.8 MO( A<, RP< ,?T -*< MO( AA, 1'RR< ,?T -*< 8?D ! 'haracter input with echo ,?T -*<, function call R*< ! 'haracter input without echo ,?T -*<, function call RO< Title @E-&! / Progra" 2A .MOD86 SMA66 .DATA M8SSA.8 M8SSA.8.STA'= -RR .'OD8 MO( MO( AA, VDATA DS, AA D& D& T8nter a character7 #,#U# TThe character you typed is7 #,RD<, RA<,#U# ! 8xit to DOS

68A DA, M8SSA.8 MO( A<, RP< ,?T -*< ! Display message MO( A<, RMO( D6, A6 ,?T -*< 68A DA, M8SSA.8 MO( A<, RP< ,?T -*< MO( A<, RO< ,?T -*< MO( &6, A6 ! 9unction read character without echo. ! 'haracter read is returned in A6 register. ! ?o echo on the display. ! Sa)e character read in &6 register ! 9unction R-<, display character ! 6oad character to be displayed

68A DA, M8SSA.8MO( A<, RP< ! Display M8SSA.8,?T -*< MO( MO( A<, RD6, &6 ! 9unction R-<, display character ! 6oad character to be displayed from &6 Page 2 o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

,?T -*< MO( A<, 1'< ,?T -*< 8?D ! 8xit to DOS

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3 o) *+

Experiment # 3 Memory and Program Segmentation


Introduction: ,n this experiment you will be introduced to physical segmentation of the memory, and the logical segmentation of programs. @ou will also deal with the different addressing modes, and learn how to calculate the physical and offset addresses. Objectives: *+ Addressing modes in the OROJ processor -+ Segmentation7 "hysical Segments and 6ogical Segments. Structure o) an Asse"bl# $anguage Progra": An assembly language program is written according the following structure and includes the following assembler directi)es7

TIT$E @O&tional: 4rite t%e Title o) #our &rogra"A !MODE$ SMA$$ Assembler directi)e that defines the memory model to use in the program. The memory model determines the si>e of the code, stac% and data segments of the program !STAC8 Assembler directi)e that reser)es a memory space for program instructions in the stac% !DATA Assembler directi)e that reser)es a memory space for constants and )ariables !CODE Assembler directi)e that defines the program instructions E0D Assembler directi)e that finishes the assembler program Eac o! t e segments is called a logical segment. "epending on t e memory# t e code and data segments may be in t e same or in di!!erent p ysical segments according to table 3.3.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page + o) *+

Me"or# Model Si$e o! %ode and "ata T,?@ SMA66 M8D,0M 'OM"A'T 6A;.8 <0.8 Stac, Directive: 'ode and data no more than J1=& combined 'ode and data segments must be no more than J1=& each 'ode can be more than J1=&, data still limited to no more than J1=& 'ode limited to no more than J1=&, data can be more than J1=& 'ode and data can each be more than J1=, no array can be larger than J1=& 'ode and data can each be more than J1=&, arrays can be larger than J1=& Table 2! 7 Memory Models

Directi)e is .stac, for stac% segment Should be declared e)en if program itself doesnFt use stac% needed for subroutine calling (return address and possibly passing parameters May be needed to temporarily sa)e registers or )ariable content $ill be needed for interrupt handling while program is running

Me"or# allocation:

Directi)e is !data for data segment All )ariables must be declared, and memory space for each allocated. Data definition directi)e can be followed by a single )alue, or a list of )alues separated by commas Different data definition directi)es for different si>e types of memory *.D& + define byte (O bits -.D$ + define word (*J bits /.DD + define double word (/- bits 1.DE + define 3uad word (J1 bits

Code Seg"ent:

Directi)e is !code for code segment The GprogramG resides here Directi)e is End Tells assembler that this is the end of the program

End o) Progra": + + 0ote7

The se3uence of instructions at the beginning of a program used to assign the data segment7 MO( AA, VDATA MO( DS, AA May be replaced by the following directi)e7 !STARTUP which assigns both DATA and 'OD8 segments, and hence no warning will be issued by the assembler. <owe)er, it should be noted that the program would start at address 'S7RR*N<. The Startup directi)e occupies the bytes 'S7RRRR to 'S7RR*N<. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page ? o) *+

,dentically, the se3uence used to terminate and exit to DOS can be replaced by the !E7IT directi)e, which has exactly the same effect. Pre $ab 4or,7 *. -. /. 1. 2. $ab 4or,: *+ Assemble, 6in% and ;un program *. -+ 0se 'ode(iew Debugger to fill in the table associated with program /.*. /+ 'alculate both the effecti)e and physical addresses of each instruction. "ut the results on the gi)en table. 1+ Assemble, 6in% and ;un program -. 2+ 9ill in table -, associated with program -, in which you specify only the addressing mode, for both source and destination, for each instruction. J+ Show all tables to the instructor. N+ Submit all your wor% at the end of the lab session. $ab Assign"ent: $rite a program that prompts the user to enter a string, in capital letters, of a maximum length of -R characters. ;ead the string in capital letters and con)ert it to small letters. Then display the new string. 0ote7 To con)ert a capital letter to a small one, use the following instruction7 !;ead character MO( A6, characterWread ADD A6, -R< ! Display character in A6 register 0se the following to loop through the string you :ust entered. Again7 MO( 'A, ?umberWofWbytesWread Start loop here ! 'on)ert to small letters. 6OO" Again ! This program displays a string terminated by a U sign using ,?T -*< function RP<. TIT$E @Progra" 2. A .MOD86 SMA66 .STA'= -RR .DATA M8SSA.8 M8SSA.8Study the attached hand out, and re)iew the material related to segmentation and addressing modes. $rite programs /+* and /+$rite the program gi)en in assignment. 9ill in the tables associated with the different programs. &ring your wor% to the lab.

D& D&

T This is the message to be displayed7 #, TU# T The message you ha)e :ust entered 7 # , TU# Page = o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

&09 .'OD8

D& *R D& *R D0"(M

!?umber of characters to be read ! ;eser)e *R bytes for string

MO( AA, VDATA MO( DS, AA 68A DA, M8SSA.8 MO( A<, RP< ,?T -*< MO( A<, RA< MO( DA, O99S8T &09 ,?T -*< 68A DA, M8SSA.8MO( A<, RP< ,?T -*< 68A DA, &09 MO( A<, RP< ,?T -*< MO( AA, 1'RR< ,?T -*< 8?D TIT$E @PRO:RAM / E7PERIME0T 2A ! This program displays a message and reads a new message from the %eyboard .MOD86 SMA66 .STA'= -RR .DATA ';69 ";OM"T ST;,?.* ST;,?.-

D& D& D& D&

RD<, RA<,FUF F8nter a name of max. length /R char.7 F,RD<,RA<,FUF FMr. F,FUF F studies OROJ programming. F,FUF

! Allocate /- bytes for &0998;, and put the )alue /* in the second byte. &0998; D& /*, /- D0"(M .'OD8 .STA;T0" 68A DA, ";OM"T MO( A<, RP< ,?T -*< MO( A<, RA< 68A DA, &0998; ,?T -*< 68A DA, ';69 MO( A<, RP< ,?T -*< 68A MO( DA, ST;,?.* A<, RP< ! This directi)e initiali>es the DS and 'S segments. ! display prompt

! read into buffer

! mo)e cursor to next line

! display string*

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ; o) *+

,?T -*< ! ?ow display the buffer i.e. what has been read. MO( A<, RP< MO( &<, RR< MO( &6, &0998;B*C MO( &0998;B&AS-C, FUF 68A DA, &0998;B-C ,?T -*< 68A DA, ST;,?.MO( A<, RP< ,?T -*< 68A DA, ';69 MO( A<, RP< ,?T -*< MO( A<, R-< MO( D6, &0998;B*C ADD D6, /R< ,?T -*< MO( AA, 1'RR< ,?T -*< 8?D Student ?ame7 Student ,D7 Instruction MO( AA,VDATA MO( DS,AA 68A DA,M8SSA.8 MO( A<,RP< ,?T -*< MO( A<, RA< MO( DA, O99S8T &09 ,?T -*< 68A DA,M8SSA.8MO( A<,RP< ,?T -*< 68A DA, &09 MO( A<,RP< ,?T -*< MO( AA,1'RR< ,?T -*< S % i p Page * o) *+ &efore ,?T -*< ," D After ,?T -*< ," D &efore ,?T -*< ," D After ,?T -*< ," D &efore ,?T -*<, ," D After ,?T -*<, ," D E-&eri"ent 29 Progra" I : Date7 Section7 Source Destination Address( Register Content Address( Register Contents 5e)ore A)ter

! mo)e in &6 buffer length ! put a U sign at the end of &0998; ! load actual length of buffer ! display string-

! mo)e cursor to next line

! display number of characters read , less than *R ! read second byte of buffer ! con)ert to number

Addressing Mode

&efore ,?T -*< ," D

After ,?T -*< ," D

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

E-&eri"ent 29 Progra" I/: Student ?ame7 Student ,D7 Date7 Section7 Addressing Modes ,nstructions Source 68A DA,";OM"T MO( A<,RP< ,?T -*< MO( A<,RA< 68A DA, &0998; ,?T -*< 68A DA, ';69 MO( A<,RP< ,?T -*< 68A DA,ST;,?.* MO( A<,RP< ,?T -*< MO( A<,RP< MO( &<,RR< MO( &6,&0998;B*C MO( &0998;B&AS-C,FUF 68A DA,&0998;B-C ,?T -*< 68A DA,ST;,?.MO( A<,RP< ,?T -*< 68A DA, ';69 MO( A<,RP< ,?T -*< MO( A<, R-< MO( D6,&0998;B*C ADD D6, /R< ,?T -*< MO( AA,1'RR< ,?T -*< Destination

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /< o) *+

Experiment # & Indexing and "ata Manipulation


Introduction: ,n this experiment you will be introduced to data transfer and arithmetic instructions. @ou will also deal with indexing, and array manipulation. Objectives: *+ &asic arithmetic instructions -+ 0se of the AS',, table. /+ Manipulation of arrays Addressing Modes: The following table summari>es all addressing modes used by the OROJ processor. Addressing Mode ;egister ,mmediate Direct ;egister+,ndirect &ased ,ndexed &ased+,ndexed Source o&erand Assuming7 DS D *RRR<, &A D R-RR<, S, D R/RR< T#&e Address :eneration Address MO( AA,&A ;egister + + MO( AA, R9N< ,mmed. + + MO( AA,B*-/1<C Mem. DS x *R< S*-/1< **-/1< MO( AA,B&AC Mem. DS x *R< SR-RR< *R-RR< MO(AA,B&ASRJC Mem. DS x *R< SR-RR< S RRRJ< *R-RJ< MO(AA,BS,SRJC Mem. DS x *R< SR/RR< S RRRJ< *R/RJ< MO( AA,B&ASS,SRJC Mem. DS x *R< SR-RR< SR/RR< S *R2RJ< RRRJ< Table 3! : Addressing modes E-a"&le

ASCII code Table: The AS',, table is a double entry table, which contains all alphanumeric characters and symbols. 8ach symbol, or character, has its own AS',, code. This code can either be in decimal or hexadecimal format. The code of a gi)en character is found by concatenating the column number with the row number, if the code is to be expressed in hexadecimal. The row number is to be the least significant. 9or the same code to be expressed in decimal, the row number is added to the column number (See example gi)en below . As an example, the symbol# U# is at the intersection of row 1 and column -, therefore its AS',, code is -1<. The decimal e3ui)alent of this code can be found by adding 1 to /-, which yields /J. The following tables show the AS',, codes (Table 1.* , and examples on the use of the AS',, table (Table 1.- , and how to calculate the AS',, codes for different characters and symbols. C%aracter a A H Colu"n I J 1 8 Row I * * * 2 Code JBK J* 1* 8* -2 Code J <K PJ S * D PN J1 S * D J2 --1 S * D --2 /- S 2 D /N

Table 3!27 8xamples on the use of the AS',, table7

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page / o) *+

0ote on t%e use o) arra#s: The D& and D$ directi)es are respecti)ely used to declare a )ariable of si>e byte or word. The following declaration defines a )ariable A of si>e byte and assigns it the )alue *R<7 A D& *R< ,dentically the following will define a )ariable of si>e word, and assigns it the )alue */89<7 @ D$ */89< The D0" directi)e may be used to reser)e more than one consecuti)e data item and initiali>e reser)ed items to the same )alue. 9or example, the instruction7 &yteArray D& *RR D0"(R

,nstructs the assembler to reser)e an array of *RR bytes, and initiali>es each byte to the )alue >ero. ,f the 4R5 in the abo)e declaration is replaced with 4M5, the assembler will not initiali>e the bytes of the array to any )alue. To access the different elements of an array, we use one of the following addressing modes (See 8xperiment Q / . &ased addressing mode. ,ndexed addressing mode. &ased+,ndexed addressing mode.

The &ased+,ndexed addressing mode may be used to access a two+dimensional array. <ere are examples of each case. Array* D& R,*,-,/,1,2,J,N,O,P Array- D& *R D0"(R Array/ D& **,*-,*/,-*,--,-/,/*,/-,// ;owSi>e 8E0 / 5ased addressing "ode: MO( MO( Inde-ed addressing "ode: &A, O99S8T Array* A6,B&AS1C ! Address Array* ! Access 2th element of Array*

MO( D,, O99S8T Array! Address ArrayMO( BD,SJC, A6 ! 'opy to Nth element of ArrayMO( S,, / MO( Array-BS,C, A6 !'opy to 1th element of Array5ased.Inde-ed addressing "ode: MO( MO( MO( MO( MO( &A, O99S8T Array/ ! Address Array/ S,,*X;owSi>e ! &eginning of -nd row D,,-X;owSi>e ! &eginning of /rd row A6, B&ASS,S*C ! Access -nd element of -nd row B&ASD,S-C, A6 ! Access /rd element of /rd row

Re"ar,7 ?otice that row ;, has index (;+* , and element n has index (n+* .

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page // o) *+

Pre $ab 4or,7 *. -. /. Study programs 1.* and 1.-, and re)iew the material related to indexing and data manipulation. $rite both programs and see how program 1.* manipulates the )ariables in internal registers, and how program 1.- uses memory for the same purpose. Modify program 1.* so that it adds two numbers of two digits each. 0se only registers, and ma%e sure to ta%e care of the carry when adding the two most significant digits. 'all this program 1./.

0ote: ,n this case try to understand how the program reads the numbers and how it manipulates them. This will help you in writing your program. As a %int, one should %now that numbers are gi)en in decimal to the program. 1. Modify program 1./ so that it can handle numbers of four digits. 0se arrays in this case. 'all this program 1.1. 2. &ring your wor% to the lab. $ab 4or,: *+ Assemble, 6in% and ;un program *. -+ <ow many digits can you enter each timeM 8xplain this. /+ $hat happens when the sum exceeds PM 8xplain this. 1+ Assemble, 6in% and ;un program -. Dress a table and show some inputs and outputs. 2+ ;epeat step 1 with program /. J+ Show all your wor% to the instructor. N+ Submit all your wor% at the end of the lab session. $ab Assign"ent: $rite a program that prompts the user to enter two numbers of 1 digits each. Then the program calculates the 3uotient and remainder of the di)ision of the two numbers. The two numbers are entered as two arrays of si>e four (1 . TIT$E DPRO:RAM E7PERIME0T 3D

! This program reads two numbers from the %eyboard and ! gi)es their sum. This program uses internal registers ! to store the )ariables. .MOD86 SMA66 .STA'= -RR .DATA ';69 D& RD<,RA<,FUF ";OM"T* D& F8nter the first positi)e integer7 F,FUF ";OM"TD& F8nter the second positi)e integer7 F,FUF ";OM"T/ D& FThe sum of the two numbers is7 F,FUF .'OD8 .STA;T0" 68A DA, ";OM"T* ! D,S"6A@ ";OM"T* MO( A<, RP< ,?T -*< MO( A<, R*< ! ;8AD 9,;ST ?0M&8; Page /2 o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

,?T -*< S0& A6, /R<! 'on)ert character to number MO( '6, A6 ! SA(8 T<8 ?0M&8; ,? '6 68A DA, ';69 ! MO(8 '0;SO; TO ?8AT 6,?8 MO( A<, RP< ,?T -*< 68A DA, ";OM"T- ! D,S"6A@ ";OM"TMO( A<, RP< ,?T -*< MO( A<, R*< ! ;8AD S8'O?D ?0M&8; ,?T -*< S0& A6, /R ! 'on)ert character to number ADD A6, '6 ! "8;9O;M ADD,T,O? A?D SA(8 ;8S06T ,? '6 MO( ADD '6, A6 '6, /R<! 'O?(8;T D,.,T TO '<A;A'T8;

68A DA, ';69 ! MO(8 '0;SO; TO ?8AT 6,?8 MO( A<, RP< ,?T -*< 68A DA, ";OM"T/ ! D,S"6A@ ";OM"T/ MO( A<, RP< ,?T -*< MO( D6, '6 ! D,S"6A@ S0M MO( A<, R-< ,?T -*< .8A,T 8?D TIT$E DPRO:RAM / E7PERIME0T 3D ! This program reads two numbers from the %eyboard and ! displays their sum. This program uses the memory to ! store the )ariables. .MOD86 SMA66 .STA'= -RR .DATA ';69 D& RD<,RA<,FUF ";OM"T* D& F8nter the first positi)e integer7 F,FUF ";OM"TD& F8nter the second positi)e integer7 F,FUF ";OM"T/ D& FThe sum of the two numbers is7 F,FUF ?0M* D& M ?0MD& M ;8S D& M .'OD8 .STA;T0" 68A DA, ";OM"T* MO( A<, RP< ,?T -*< MO( A<, R*< ,?T -*< S0& A6, /R< MO( ?0M*, A6 ! D,S"6A@ ";OM"T* ! ;8AD 9,;ST ?0M&8; ! 'on)ert character to number ! SA(8 ?0M* Page /3 o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

68A DA, ';69 MO( A<, RP< ,?T -*< 68A DA, ";OM"TMO( A<, RP< ,?T -*< MO( A<, R*< ,?T -*< S0& A6, /R< MO( ?0M-, A6 ADD A6, ?0M* MO( ;8S, A6 68A DA, ';69 MO( A<, RP< ,?T -*< 68A DA, ";OM"T/ MO( A<, RP< ,?T -*< MO( D6, ;8S ADD D6, /R< MO( A<, R-< ,?T -*< .8A,T 8?D

! MO(8 '0;SO; TO ?8AT 6,?8 ! D,S"6A@ ";OM"T! ;8AD S8'O?D ?0M&8; ! 'on)ert character to number ! SA(8 ?0M! "8;9O;M ADD,T,O? ! SA(8 ;8S06T ,? ;8S ! MO(8 '0;SO; TO ?8AT 6,?8 ! D,S"6A@ ";OM"T/ ! D,S"6A@ S0M ! ;8T;8,(8 ;8S 9;OM M8MO;@ ! 'O?(8;T D,.,T TO '<A;A'T8;

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /+ o) *+

Experiment #' Arit metic and Logic Instructions


Introduction: ,n this experiment, you will be introduced to the arithmetic and logic instructions of the OROJ family of processors. @ou will also deal with the con)ersion of numbers from one radix to another. Objectives: *+ 6ogic ,nstructions -+ &ase con)ersion Arit%"etic Instructions: The following table summari>es the arithmetic instructions used with the OROJ microprocessor. ,t also shows the effect of each instruction, a brief example, and the flags affected by the instruction. The 4X5 in the table means that the corresponding flag may change as a result of executing the instruction. The 4+4 means that the corresponding flag is not affected by the instruction, whereas the 4M5 means that the flag is undefined after executing the instruction. 1lags A))ected S9 L9 A9 "9 X X X X X X X X X M X X X X X X X X X M X X X X X X X X X M X X X X X X X X X M

Type Addition

Instruction ADD AD' ,?' DAA S0& S&&

E-a"&le ADD AA,N&< AD' AA,N&< ,?' B&AC DAA S0& '6,A< S&& '6,A< D8' DAT DAS ?8. 'A M06 '6 M06 'A ,M06 &@T8 "T; A ,M06 $O;D "T; A D,( $O;D "T; A

Meaning AA AA S N& AA AA S N& S'9 B&ACB&ACS* '6 '6 Y A< '6 '6 Y A< Y '9 BDATC BDATC Y * 'A R Y 'A AA A6 X '6 (DA,AA AAX 'A AA A6 X BAC (DA,AA AAX BAC

O9 X X X M X X X M X X

'9 X X + X X X + X X X

Subtraction

D8' DAS ?8. M06

Multi&lication ,M06

Division

Sign E-tension

AA E((BDA,AAC KBAC D,( M M M M M DA ;((BDA,AAC KBAC A6 E(AAK&< ,D,( ,D,( &< M M M M M A< ;(AAK&< '&$ '&$ + + + + + A< MS&(A6 '$D '$D + + + + + DA MS&(AA Table +! :7 Summary of Arithmetic ,nstructions of the OROJ microprocessor

M + +

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /? o) *+

0otes: The DAA (Decimal Ad:ust after Addition instruction allows addition of numbers represented in O+bit pac%ed &'D code. ,t is used immediately after normal addition instruction operating on &'D codes. This instruction assumes the A6 register as the source and the destination, and hence it re3uires no operand. The effect of DAS (Decimal Ad:ust after Subtraction instruction is similar to that of DAA, except the fact that it is used after performing a subtraction. '&$ and '$D are two instructions used to facilitate di)ision of O and *J bit signed numbers. Since di)ision re3uires a double+width di)idend, '&$ con)erts an O+bit signed number (in A6 , to a word, where the MS& of A6 register is copied to A< register. Similarly, '$D con)erts a *J+bit signed number to a /-+ bit signed number (DA,AA . $ogic Instructions: 6ogic shift and rotate instructions are called bit manipulation operations. These operations are designed for low+le)el operations, and are commonly used for low+le)el control of inputKoutput de)ices. The list of the logic operations of the OROJ is gi)en in *, along with examples, and the effect of these operations on the flags. The 4X5 in the table means that the corresponding flag may change as a result of executing the instruction. The 4+5 means that the corresponding flag is not affected by the instruction, whereas the 4M5 means that the flag is undefined after executing the instruction.

1lags Instruction E-a"&le Meaning O1 A?D A?D AA, 99D9< O; A6, -R< AO; ?0M*, 99RR ?OT ?0MAA AA A?D 99D9< R S1 X H1 X A1 M P1 X '.'. O; AO; A6 A6 O; -R< B?0M*CB?0M*CAO; 99RR WWWWWWW B?0M-CB ?0M-C Table +!/7 Summary of the 6ogic ,nstructions of the OROJ Microprocessor The logic operations are the software analogy of logic gates. They are commonly used to separate a bit or a group of bits in a register or in a memory location, for the purpose of testing, resetting or complementing. 9or example, if b is the )alue of a certain bit in a number. The related effects of the basic operations on a single bit are indicated in Table 2./7 R R X X X X M M X D.D. X 8.8. ?OT + + + + + + R R A!A! C1 &.&. R

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /= o) *+

O&eration b A?D R D b O; * D b AO; * D b AO; R D

R * b b

E))ect ;eset the bit Set the bit 'omplement the bit + Table +!2: 8ffects on bits of the basic logic instructions

5#te "ani&ulations )or reading and dis&la#ing &ur&oses7 *. To put two decimal digits into the same byte use the following7 MO( A<, R*< ,?T -*< S0& A6, /R< MO( '<, A6 MO( A<, R*< ,?T -*< S0& A6, /R< MO( '6, A6 MO( A6, *RRRR& M06 '< AO; A<, A< O; A6, '6 ,f we want to perform addition7 ! ,f A6 contains the first number in &'D format ! and '6 contains the second number in &'D format ADD A6, '6 DAA ! Decimal ad:ust ! ?ew result in A6 in &'D format MO( '6, A6 ! ?umber in '6 register. See next how to display it as decimal number. - . To display a number in &'D format use the following7 ! The number is in the '6 register7 MO( A6, '6 ! Mo)e '6 to A6 AO; A<, A< ! 'lear A< MO( &6, *RRRR& D,( &6 ! Shift AA 1 bits to the right A?D A6, R9< ! 'lear 1 high nibbles of A6 ADD A6, /R< ! 'on)ert to character ! ?ow Display A6 as high digit first MO( A6, '6 A?D A6, R9< ADD A6, /R< ! ;ead number again ! 'lear 1 high nibbles of A6 ! 'on)ert to character

! ;ead high digit e.g. O

! ;ead low digit e.g. / ! 0se M06 by *RRRR& to shift left by 1 bits ! Shift A6 1 bits to the left ! 'lear A< ! ;esult in A6 O/

! ?ow Display A6 as low digit second

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /; o) *+

Dis&la#ing Data in an# 0u"ber 5ase r7 The basic idea behind displaying data in any number base is di)ision. ,f a binary number is di)ided by *R, and the remainder of the di)ision is sa)ed as a significant digit in the result, the remainder must be a number between >ero and nine. On the other hand, if a number is di)ided by the radix r, the remainder must be a number between >ero and (r+* . &ecause of this, the resultant remainder will be a different number base than the input which is base -. To con)ert from binary to any other base, use the following algorithm. Algorit%"7

*. Di)ide the number to be con)erted by the desired radix (number base r .


-. /. 1. Sa)e the remainder as a significant digit of the result. ;epeat steps * and - until the resulting 3uotient is >ero. Display the remainders as digits of the result. ?ote that the first remainder is the least significant digit, while the last remainder is the most significant one.

Pre $ab 4or,7 *. -. /. 1. Study program 2.-, and explain how base con)ersion is performedM $rite, assemble and lin% program 2.*. @ou will run it in the lab using 'ode(iew. $rite, assemble, lin% and run program 2.-. Modify the program so that it prompts the user for the ;AD,A and the number ?0M to be con)erted. 'all the new program prog+2./. $rite a program that con)erts from decimal to hexadecimal. ?ame it "rog+2.1. &ring your wor% to the lab.

2.
J.

$ab 4or,: *+ 0se 'ode(iew to trace program 2.*. 9ill in table 2./. ?otice any changes in the status flags, and explain them. -+ ;un program 2.-, and see what )alue is displayed. /+ 'hange the )alue of the )ariable ?0M and see the output )alue. 1+ ?ow change the )alue of ;AD,A and see the )alue displayed. 2+ $rite a program that prompts the user to enter two numbers of 1 digits each. 'on)erts these numbers to hexadecimal. Then calculates the sum, the difference of the two numbers, and finally displays the result in decimal format. ?ame it program 2.2. J+ Show all your wor% to the instructor. N+ Submit all your wor% at the end of the lab session. $ab Assign"ent: $rite a program that reads two binary numbers of O digits each, stores them inside the internal registers. Multiply the two numbers using a simple M06 operation, and display the result in decimal format. To ease bit manipulation and shifting, use di)ision and multiplication by -, to perform right shift and left shift.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page /* o) *+

TIT$E @Progra" +! : $ogic InstructionsA ! This program shows the effect of the logic instructions .MOD86 SMA66 .STA'= -RR .DATA ?0M* D$ ?0M- D& .'OD8 .STA;T0" MO( A?D O; AO;

R9AJ-< P1<

AA, ?0M* AA, R99D9< A6, -R< ?0M*, R99RR< ?OT ?0MAO; AA, AA MO( AA, ?0M* A?D AA, RRRO< AO; AA, RROR< .8A,T 8?D

!load AA with number ?0M* !;eset Jth bit of AA !Set Jth bit of A6 !'omplement the high order byte of ?0M* !'omplement ?0M!'lear AA ! ,solate bit 1 of ?0M* !'omplement 1th bit of AA

9ill in table 2./ while running the abo)e program using 'ode(iew. Destination Content State"ent *. -. /. 1. 2. J. N. O. P. MO( AA, ?0M* A?D AA, R99D9< O; A6, -R< AO; ?0M*, R99RR< ?OT ?0MAO; AA, AA MO( AA, ?0M* A?D AA, RRRO< AO; AA, RROR< : 8ffects of 8xecuting "rogram 2.* 5e)ore A)ter O9 D9 ,9 Status 1lags S9 L9 A9 "9 '9

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2< o) *+

TIT$E @$ab E-&! I + Progra" I +!/A ! This program con)erts a number ?0M from <exadecimal, ! to a new numbering base (;AD,A . .MOD86 SMA66 .STA'= -RR .DATA ;AD,A D& *R ?0M D$ R8981<

!radix7 *R for decimal !the number to be con)erted !put here any other number. !?ote that7 R8981< D J*1*-*R T8M" D& *R D0"(M !0sed to simulate a stac%

.'OD8 .STA;T0" MO( MO( AO; MO( AO; D,S"A*7 MO( D,( MO( ,?' ,?' O; Z?L D8' D,S"A-7 MO( MO( ADD ,?T D8' D8' Z?L .8A,T 8?D

AA, ?0M 'A, R &<, &< &6, ;AD,A S,, S, DA, RRRR &A T8M"BS,C, D6 S, 'A AA, AA D,S"A* S, D6, T8M"BS,C A<, RJ< D6, /R< -*< S, 'A D,S"A-

!load AA with number ?0M !display AA in decimal !clear digit counter !clear &< !set for decimal !'lear S, register !clear DA !di)ide DA7AA by *R !sa)e remainder !count remainder !test for 3uotient of >ero !if 3uotient is not >ero

!get remainder !select function RJ< !con)ert to AS',, !display digit !repeat for all digits !exit to dos

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2 o) *+

E-&eri"ent I ? S%i)t Rotate and Gu"& Instructions


Introduction: ,n this experiment you will be introduced to the shift and rotate instructions. @ou will also practice how to control the flow of an assembly language program using the compare instruction, the different :ump instructions and the loop instructions. Objectives: *+ -+ /+ 1+ 2+ Shift ,nstructions. ;otate ,nstructions. 'ompare ,nstruction. Zump ,nstructions. 6oop ,nstructions.

T%e S%i)t O&erations: The shift operations are used to multiply or di)ide a number by another number that is a power of - (i.e. - n or - Yn . Multiplication by - is achie)ed by a one+bit left shift, while di)ision by - is achie)ed by a one+bit right shift. The Shift Arithmetic ;ight (SA; instruction, is used to manipulate signed numbers. The regular ;ight Shift (S<; of a signed number affects the sign bit, which could cause numbers to change their sign. The SA; preser)es the sign bit by filling the )acated bits with the sign of the number. Shift Arithmetic 6eft (SA6 is identical in operation to SA;. The rotate operations are )ery similar to the shift operations, but the bits are shifted out from one end of a number and fed bac% into the other end to fill the )acated bits. They are pro)ided to facilitate the shift of long numbers (i.e. numbers of more than *J bits . They are also used to reposition a certain bit of a number into a desired bit+location. The rotate right or left instructions through the carry flag (;'6 and ;'; are similar to the regular rotate instructions (;O6 and ;O; , but the carry flag is considered as a part of the number. <ence, before the rotate operation, the carry flag bit is appended to the number as the least significant bit in the case of ;'6, or as the most significant bit in the case of ;';. 1!A! 1lags O1 T#&e Instruction E-a"&le Meaning S1 H1 A1 P1 C1

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2/ o) *+

S%i)t

S<6

S<6 AA,*

Shift AA left by * bit. 9ill )acated bit with R. Shift AA left by * bit. 9ill )acated bit with R. Shift ?0M- right by the number of bits indicated in '6. 9ill )acated bits with R. As S<; but fill )acated bits with the sign bit. Same as S<6, but shifted bits are fed bac% to fill )acated bits. Same as ;O6, but carry flag is appended as MS&, and its content is shifted with the number. Same as S<;, but shifted bits are fed bac% to fill )acated bits. Same as ;O;, but carry flag is appended as 6S&, and its content is shifted with the number.

SA6

SA6 AA,*

S<;

S<; ?0M-,'6

SA;

SA; ?0M-,'6

Rotate

;O6

;O6 &<,'6

;'6

;'6 &<,'6

;O;

;O; ?0M*,*

;';

;'; ?0M*,*

Table ?! 7 Summary of the Shift and ;otate ,nstructions of the OROJ Microprocessor Co"&are instruction: The compare instruction is used to compare two numbers. At most one of these numbers may reside in memory. The compare instruction subtracts its source operand from its destination operand and sets the )alue of the status flags according to the subtraction result. The result of the subtraction is not stored anywhere. The flags are set as indicated in. Instruction E-a"&le Meaning

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 22 o) *+

CMP

'M" AA, &A

,f (AA D &A then L9 * and '9 R ,f (AA [ &A then L9 R and '9 * ,f (AA \ &A then L9 R and '9 R

Table ?! /: The 'ompare ,nstruction of the OROJ Microprocessor Gu"& Instructions: The :ump instructions are used to transfer the flow of the process to the indicated operator. $hen the :ump address is within the same segment, the :ump is called intra-segment jump. $hen this address is outside the current segment, the :ump is called inter-segment jump. An o)er)iew of all the :ump instructions is gi)en in lists the possible addressing modes used with the :ump instructions. $hereas, Table J. + gi)es examples on the use of such instructions. Instruction Type Unconditional Co"&arisons ZM" ZA ZA8 Z& Z&8 Z8 Z?8 Z. Z.8 Z6 Z68 Z'AL Z' Z?' Z?O Parit# Test Sign 5it Hero 1lag ZO o)erflow O9 D* Z?" :po no parity (parity odd "9 D R Z" :pe parity (parity e)en "9 D * Z?S no sign S9 D R ZS sign S9 D * ZL >ero L9 D * Z?L non+>ero L9 D R Table ?! 2: Zump ,nstructions of the OROJ Microprocessor Addressing Mode ,mmediate S&eci)ied 5# $ord Encoded As DifferentiallyX Directive S<O;T Meaning Jju"& i)K unconditional abo)e (not below or e3ual abo)e or e3ual (not below below (not abo)e or e3ual below or e3ual (not abo)e e3ual ( >ero not e3ual (not >ero greater (not lower or e3ual greater or e3ual (not lower lower (not greater or e3ual lower or e3ual (not greater 'A register is >ero 'arry no carry no o)erflow Condition ?one '9 D R and L9 D R '9 D R '9 D * '9 D * or L9 D * L9 D * L9 D R L9 D R and S9 D O9 S9 D O9 (S9 xor O9 D * i.e. S9 O9 (S9 xor O9 or L9 D * ('9 or L9 D R '9 D * '9 D R O9 D R

:nbe :nb :nae :na :> :n> :nle :nl :nge :ng loop

Carr# Over)low

$abel Pointer Short 0ear

Range S*-NK+*-O bytes ," ," S Offset ,ntra+segment ," Address

,mmediate ;egister Memory

$ord $ord $ord

Differentially Absolute address Absolute address ?8A; "T;

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 23 o) *+

1ar

,nter+segment ," Address 'S Segment

,mmediate Memory

Double $ord Double $ord

Absolute address 9A; "T; Absolute address

Table ?! 37 Zump ,nstructions and Addressing Modes


X

Differentially D Difference between current and next address.

Instruction ZM" G0H GE GC

E-a"&le ZM" 9A; "T; B&AC

Meaning ," B&AC, 'S B&AS-C

Z?L 8?D ,f (L9DR Then ," Offset of 8?D Z8 9,;ST ,f (L9D* Then ," Offset of 9,;ST Z' S8'O?D ,f ('9D* Then ," Offset of S8'O?D Table ?! +: 8xamples of Zump ,nstructions of the OROJ Microprocessor

T%e $OOP Instructions7 The 6OO" instruction is a combination of a D8' and Z?L instructions. ,t causes execution to branch to the address associated with the 6OO" instruction. The branching occurs a number of times e3ual to the number stored in the 'A register. All 6OO" instructions are summari>ed in Table J.J. Instruction $OOP $OOPE $OOPH $OOP0E $OOP0H E-a"&le 6OO" 6abel* 6OO"8 6abel* 6OO"?L 6abel* Meaning ,f ('AR then ," Offset 6abel* ,f ('AR and L9 D R then ," Offset 6abel* ,f ('AR and L9 D R then ," Offset 6abel* Table ?! ?: Summary of the 6OO" ,nstructions. T%e $oo& Progra" Structure9 Re&eat.Until and 4%ile.Do: 6i%e the conditional and unconditional :ump instructions which can be used to simulate the ,9+Then+8lse structure of any programming language, the 6oop instructions can be used to simulate the ;epeat+0ntil and $hile+Do loops. These are used as shown in the following. Structure Code Re&eat.Until ! ;epeat until 'A D R + MO( 'A, 'O0?T Again7 + + + + + + 4%ile.Do ! $hile ('A R Do + MO( 'A, 'O0?T Again7 ZL ?ext + + + + + Page 2+ o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

6OO" Again + +

?ext7

6OO" Again + +

Table ?! =7 The 6oop "rogram Structure. Pre $ab 4or,7 *. -. /. 1. 2. J. $ab 4or,: Show program J.* and your comments to your lab instructor. *+ ;un program J.- using 'ode(iew. -+ See what the effect of such program on ?0M* is. /+ ;un program J./, and see the effect on ?0M* after displaying ?0M-. 1+ Modify program J.-, using program J.*, such that you enter an O+bit binary number from the %eyboard, and in)ert swap the high and the low nibbles of the number, and finally display it. 'all this program J.1. 2+ Show all your wor% to the instructor, and submit it at the end of the lab session. $ab Assign"ent: $rite a program that prompts the user to enter an O bit binary number, between R and -22. The program then in)erts all bits according to the figure below. This program is useful in Signal "rocessing for the calculation of the 9ast 9ourier Transform (99T . The operation is called Decimation (in time or fre3uency , and the bit manipulation is called bit shuffling i.e. rearrangement of bits. 'omplete program J.*, according to the gi)en guidelines. 'hec% on some )alues and see if it is wor%ing properly. 'omment on the program, trying to understand how con)ersion is done. $rite program J.- and ma%e sure it contains no errors. Do the modifications gi)en in the guidelines. This will be program J./. &ring your wor% to the lab.

&it ( i &it ( N+ i

for

i DRYN

1igure ?! 7 &it Shuffling

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2? o) *+

TIT$E @E-&eri"ent ?9 Progra" A ! This program adds - binary numbers and prints the result in binary format .MOD86 SMA66 .STA'= -RR .DATA ';69 ";OM"T* ";OM"T";OM"T/ ?0M* ?0M-

D& D& D& D& D$ D$

RD<, RA<, FUF F8nter the first O+bit binary number7 F,FUF F8nter the second O+bit binary number7 F,FUF FThe sum of the two numbers in binary is7 F,FUF M M

.'OD8 .STA;T0" ! D,S"6A@ ";OM"T* ! ;8AD A?D 'O?(8;T T<8 9,;ST ?0M&8; 'A66 ;8AD MO( ?0M*,&A MO( DA, O99S8T ';69 MO( A<, RP< ,?T -*< ! D,S"6A@ ";OM"T! ;8AD A?D 'O?(8;T T<8 S8'O?D ?0M&8; 'A66 ;8AD MO( ?0M-,&A ! ;8AD 9;OM STA'= MO( &A, ?0M* ADD &A, ?0M! D,S"6A@ ";OM"T/ 'A66 ;8S06T .8A,T !XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ! ";O' ;8AD A ?0M&8; A?D 'O?(8;T ,T TO &,?A;@ ;8AD ";O' ?8A; MO( &A, RRRR MO( 'A, RRRO MO( A<, R*< 6*7 ,?T -*< S0& A6, /R< S<6 &6, * O; &6, A6 6OO" 6* AO; &<, &< ;8T ;8AD 8?D" ! ADD T<8 T$O ?0M&8;S ! ;8AD 9;OM STA'=

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2= o) *+

E-&eri"ent I = Subroutine Bandling Instructions and Macros


Introduction: ,n this experiment you will be introduced to subroutines and how to call them. @ou will )erify the exchange of data between a main program and a subroutine in the OROJ en)ironment. @ou will also use Macros, and as applications you will deal to a useful data representation7 loo%+up tables. . @ou will need some of the programs de)eloped in pre)ious experiments to rewrite them in a more structured way. Objectives: *+ -+ /+ 1+ 2+ "rocedures and "rocedure 'alls "arameter "assing through Memory, ;egisters and the Stac% 0se of Macros 6oo%+up Tables ;eal+time cloc% reading,

Macros: Macro se3uences relie)e the programmer from retyping the same instructions. They allow you create your own pseudo language for instruction se3uences that often appear in programming. A macro se3uence starts by the MA';O directi)e and ends by an 8?DM directi)e. Associated with MA';O is the name of the macro and any parameters that are carried with the macro to the instructions between MA';O and 8?DM statements. "rogram N./ contains two macros. A MA';O is declared and used as shown in the following example DISP$AL MACRO STRI0: MO6 D79O11SET STRI0: MO6 AB9<*B I0T / B E0DM MI) @MessageAis t%e string to be dis&la#ed9 t%e M Macro is called as )ollows:

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 2; o) *+

DISP$AL MESSA:E Macros can be sa)ed in a separate file, to which a name such as 4MA';O.,?'5 can be gi)en. This file can then be used as a library, and therefore can be included in the program using the directi)e ,?'60D8, in the following manner7 ,?'60D8 MA';O.,?' "ro)ided that both, the program and the macro library are in the same directory. Alternati)ely the path has to be specified as follows7 ,?'60D8 "athIMA';O.,?' $abels local to a Macro7 $hen a MA';O contains labels, and the Macro is used more than once in a program, which is usually the case, the assembler gi)es the following error7 6abel referenced more than once. To a)oid such an error, these labels should be made local to the MA';O, this is done using the following7 DISP$AL MACRO STRI0: $ocal $abel N $abel :N N E0DM T%e Stac,: The stac% is a special segment in memory used to facilitate subroutine handling. The SS register contains the Stac% Segment number where the stac% is stored. The G.STA'=G directi)e instructs the assembler to reser)e a stac% segment of a desired si>e. 9or example, to reser)e a stac% segment of si>e OR bytes, 4.STA'= 2R5 is used before the G.'OD8G directi)e. ,n this case, the 4.STA'=5 directi)e initiali>es the Stac% "ointer to 2R<. ,f the 4.STA'=5 directi)e is missing from a program, the assembler issues the warning7 46,?=7 $arning 61R-*7 no stac% segment5. The stac% always starts at a high address and grows towards the beginning of the stac% segment at a lower address. $hen a program starts, the stac% is empty, and its si>e is >ero. The microprocessor stores data on the stac% as needed, and uses the S" register to point to the last item stored on the stac%. The stac% si>e dynamically changes as data is stored or retrie)ed from the stac%. The stac% handling instructions are summari>ed in Table N. *. The "0S< instruction is used to store the content of a *J+bit register, or memory location, on the stac%. ,t first decreases the content of S" by two and then stores the data into the two bytes on the top of the stac%. The high order byte of the data goes to the high addressed byte in the stac%. Instruction E-a"&le Meaning "0S< "0S< AA BS"C A< BS"+*C A6 S" S" Y"O" "O" ?0M* B?0M*C BS"C B?0MS*C BS"S*C S" S" S "0S<9 "0S<9 BS"+*C MS&(9; BS"+-C 6S&(9; S" S" Y"O"9 "O"9 6S&(9; BS"C MS&(9; BS"S*C S" S" S Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page 2* o) *+

0ote: 9; D 9lag ;egister Table =! 7 Summary of the Stac% <andling ,nstructions The "0S<9 instruction is similar to the "0S< instruction, except that the "0S<9 is used to push the contents of the flag register onto the stac%. The "O" and "O"9 instructions ha)e a re)erse action of the "0S< and "0S<9, respecti)ely. The "O" instruction retrie)es a word from the stac% and then increases S" by two. The "O"9 has the same effect, except that the word retrie)ed is sa)ed to the flag register. Subroutine calls7 A procedure is a reusable section of the software that is stored in memory once, but used as often as necessary. The 'A66 instruction lin%s to the procedure and the ;8T (return instruction returns from the procedure. The Stac% stores the return address whene)er a procedure is called during the execution of a program. The 'A66 instruction pushes the address of the instruction following the 'A66 (return address onto the stac%. The ;8T instruction remo)es an address from the stac%, so the program returns to the instruction following the 'A66. $ith the Assembler (MASM there are specific ways for writing, and storing, procedures. A procedure begins with the ";O' directi)e and ends with the 8?D" directi)e. 8ach directi)e appears with the name of the procedure. The ";O' directi)e is followed by the type of the procedure7 ?8A; (intra+segment or 9A; (inter+segment . ,n MASM )ersion J.A, a ?8A; or 9A; procedure can be followed by the 0S8S statement. The 0S8S statement allows any number of registers to be automatically pushed onto the stac% and popped from the stac% within the procedure. "rocedures that are to be used by all software ( global should be written as 9A; procedures. "rocedures that are used by a gi)en tas% (local are normally defined as ?8A; procedures. T%e CA$$ Instruction: The 'A66 instruction transfers the flow of the program to the procedure. The 'A66 instruction differs from the :ump instruction in the sense that a 'A66 sa)es a return address on the stac%. The ;8T instruction return control to the instruction that immediately follows the 'A66. There exist two types of calls7 9A; and ?8A;, and two types of addressing modes used with calls, ;egister and ,ndirect Memory modes. 0ear CA$$: A near 'A66 is three bytes long, with the first byte containing the opcode, and the two remaining bytes containing the displacement or distance of /- =. $hen a ?8A; 'A66 executes, it pushes the offset address of the next instruction on the stac%. The offset address of the next instruction appears in the ," register. After sa)ing this address, it then adds the displacement from bytes - and / to the ," to transfer control to the procedure. A )ariation of ?8A; 'A66 exists, 'A66?, but should be a)oided. 1ar CA$$: The 9A; 'A66 can call a procedure anywhere in the system memory. ,t is a fi)e+byte instruction that contains an opcode followed by the next )alue for the ," and 'S registers. &ytes - and / contain the new contents of ,", while bytes 1 and 2 contain the new contents for 'S. The 9A; 'A66 instruction places the contents of both ," and 'S on the stac% before :umping to the address indicated by bytes - to 2 of the instruction. This allows a call to a procedure anywhere in memory and return from that procedure. A )ariant of the 9A; 'A66 is 'A669 but should be a)oided. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page 3< o) *+

CA$$s wit% register o&erand7 'A66s can contain a register operand. An example is 'A66 &A, in which the content of ," is pushed into the stac%, and a :ump is made to the offset address located in register &A, in the current code segment. This type of 'A66 uses a *J+bit offset address stored in any *J+bit register, except the segment registers. "rogram N.* illustrates the use of the 'A66 register instruction to call a procedure that begins at offset address D,S". The offset address D,S" is placed into the &A register, then the 'A66 &A instruction calls the procedure beginning at address D,S". This program displays 4O=5 on the monitor screen.

CA$$s wit% Indirect Me"or# Address7 A 'A66 with an indirect memory address is useful when different subroutines need to be chosen in a program. This selection process is often %eyed with a number that addresses a 'A66 address in a loo%up table. "rogram N.- shows three separate subroutines referenced by ?umber *,- and / as read from the %eyboard. The calling se3uence ad:usts the )alue of A6 and extends it to a *J+bit number before adding it to the location of the loo%up table. This references one of the three subroutines using the 'A66 TA&68B&AC instruction. $hen this program executes, the letter A is displayed when a * is typed, & if - and ' if / is typed. The 'A66 instruction can also reference far pointers if the data in the table are defined as double+word data with the DD directi)e, using the 'A66 9A; "T;BS,C or 'A66 TA&68BS,C instructions. These instructions retrie)e a /-+bit address from the data segment memory location addressed by S, and use it as the address of a far procedure. Para"eter Passing7 To pass data (parameters between the main program and the routines, data may be left in the general+ purpose registers. This method has the disad)antage of changing the contents of the registers e)ery time the subroutine is called. A more elegant way is to exchange data through the stac%, or through memory. The data to be passed to a subroutine is sa)ed in the memory before calling the subroutine. All the registers that need to be sa)ed, and are used by the subroutine, should also be sa)ed and retrie)ed afterwards.

Instruction CA$$

E-a"&le 'A66 SE;T

RET

;8T

E))ect BS"+*C /1 BS"+-C 2& S" S"+," /1AR 6S&(," BS"C MS&(," BS"S*C S" S" S -

Note:

Assuming SQRT is a Near Procedure starting at !S:"#A$% and t&e instruction !A'' is at !S:"#()%.

Table =! /: Summary of the Subroutine <andling ,nstructions Reading t%e S#ste" Date: 1unction /AB9 I0T / B:

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3 o) *+

9unction -A< of ,nterrupt ,?T -*< is used to read the system date. ,t returns the Day of the wee% in A6 register, the year in 'A register, the month in D< register and the day of the month in D6 register. ?ote that as indicated in Table N. *, the returned )alues are in hexadecimal format, which, in order to be displayed, need to be con)erted to decimal, as indicated in experiment 2. E))ect7 ;ead system date 1unction E0TRL /AB 0ote A< D -A< E7IT A6 D Day of the wee% 'A D @ear (*POR+-RPP D< D Month D6 D Day of the month The day of the wee% is encoded as Sunday D RR through Saturday D RJ. The year is a binary number e3ual to *POR through -RPP. Table =! 3: ;ead Time and Date 9unction7 -A<, ,?T -*< 0ote on t%e use o) t%e 7$AT instruction: The A6AT instruction adds the contents of A6 to the contents of &A to form an address in a loo%+up+table. ,t then transfers the contents of the data at that address B&ASA6C to the A6 register. MO( &A, O99S8T TA&68 ! &A Offset TA&68 MO( A6, ,?D8A ! A6 ,ndex of the element in table A6AT ! A6 B&ASA6C ! A6 will contain the data in TA&68 at index ,?D8A! Pre $ab 4or,7 *. -. /. 1. $ab 4or,: *+ Show programs N./ and N.1 to your lab instructor. -+ Modify program J./, from experiment J, using "rocedures and Macros. 'all it program N.2. /+ "rogram N.1 reads a string and encrypts it. 'omplete the program and use Macros and "rocedures. 1+ Modify "rogram N.1, so that it reads an encrypted string and con)erts it bac% to the original one. $rite this program using procedures and Macros. 'all it program N.J. $ab Assign"ent: DOS 9unction -'< reads the system time, and wor%s as described below7 MO( A<, -'< ,?T -*< ! and returns (in binary the time as follows7 '<7 hours (R+-/ ! '67 minutes (R+2P ! D<7 seconds (R+2P ! and Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page 3/ o) *+ $rite, assemble, lin% and run program N.* and N.-. Try to understand how the different routines are written and how they are called. See also how the different procedures pass parameters between them. $rite, assemble, lin% and run program N./. See how Macros are used. ;ewrite program J.*, from 8xperiment J, using "rocedures and Macros. 'all it program N.1. &ring your wor% to the lab.

D67 hundredths of a second. 0se program N./, and the abo)e function, to de)elop a program that reads the date, and displays it in the following format7 Today is7 Sunday -1KOctoberK*PPP, The Time is7 *-7R-7/Ma%e the program loop for a large number of times, so that you can see the time changing. TIT$E @Progra" =! A ! a program that display O= on the monitor screen using procedure D,S" .MOD86 T,?@ .'OD8 .STA;T0" MO( MO( 'A66 MO( 'A66 ! select T,?@ model ! indicate start of 'OD8 segment ! indicate start of program &A, O99S8T D,S" D6, FOF &A D6, F=F &A ! address D,S" with &A ! display FOF ! display F=F

.8A,T ! exit to DOS ! ! a procedure that displays the AS',, contents of D6 on the monitor screen. ! XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX D,S" ";O' ?8A; MO( A<, R! select function R-< ,?T -*< ! execute DOS function ;8T ! return from procedure D,S" 8?D" 8?D ! end of program ! WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW TIT$E @Progra" =!/A ! program that uses a 'A66 loo%up table to access one of three different procedures7 ! O?8, T$O, or T<;88. .MOD86 SMA66 .DATA TA&68 D$ D$ D$ .'OD8 O?8 ";O' MO( MO( ,?T ;8T O?8 8?D" T$O ?8A; A<, D6, FAF -*< O?8 T$O T<;88 ! select SMA66 model ! indicate start of DATA segment ! define loo%up table

! indicate start of 'OD8 segment ! display a letter A

";O' ?8A; MO( A<, MO( D6, F&F

! display letter &

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 32 o) *+

T$O

,?T -*< ;8T 8?D" ?8A; A<, D6, F'F -*< ! display the letter '

T<;88 ";O' MO( MO( ,?T ;8T T<;88 8?D"

! Start of Main "rogram .STA;T0" TO"7 MO( A<, * ! read %ey into A6 ,?T -*< S0& A6, /*< ! con)ert from AS',, to R, *, or Z& TO" ! if below R 'M" A6, ZA TO" ! if abo)e MO( A<, R ! form loo%up address MO( &A, AA ADD &A, &A 'A66 TA&68 B&AC ! call procedure O?8, T$O, or T<;88 .8A,T ! exit to DOS 8?D ! end of file ! WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW TIT$E @Progra" =!2A ! This program uses the function read time and displays the current day .MOD86 SMA66 .STA'= *RR .DATA ';69 ";OM"T* DA@ DR D* DD/ D1 D2 DJ .'OD8 .STA;T0" ! Display "rompt* MO( A<, -A< ,?T -*< MO( S,, O99S8T DA@ MO( A<, RR ADD AA, AA ADD S,, AA MO( DA, BS,C Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing ! .8T S@ST8M DAT8 D$ D& D& D& D& D& D& D& ! select SMA66 model D& D& RD<,RA<,FUF FToday is 7 F,FUF

DR,D*,D-,D/,D1,D2,DJ TS0?DA@#,#U# TMO?DA@#,#U# TT08SAD@#,#U# T$8D?8SDA@#,#U# TT<0;SDA@#,#U# T9;,DA@#,#U# TSAT0;DA@#,#U#

Page 33 o) *+

MO( A<, RP< ,?T -*< 68A DA, ';69 MO( A<, RP< ,?T -*< ! MO(8 '0;SO; TO ?8AT 6,?8

.8A,T 8?D !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TIT$E @Progra" =!3A ! This program reads a string of -RR characters maximum and encrypts ! it. .MOD86 SMA66 .STA'= *RR .DATA ';69 D& RD<,RA<,FUF ";OM"T* D& F8nter a string 7 F,FUF ST;,?. D& *RR D0"(M 'OD8D D& *RR D0"(M 0TA& D& FM?&('AL6=Z<.9DSA"O,0@T;8$E(F 6TA& D& Fbgtnhym:u%ilop)frcdexsw>a3F .'OD8 .STA;T0" ! D,S"6A@ ";OM"T* !;ead a string from the %eyboard, sa)e it in the array ST;,?. ! Scan the string ST;,?., and do the following7 ! if character is an upper case letter ! that is7 ! if A6 \D FAF and A6 D[ FLF MO( &A, O99S8T 6TA& S0& A6, 1*< A6AT ! Sa)e the character in A6 in the array 'OD8D. ! if character is a lower case letter ! i.e. ! if A6 \D FaF and A6 D[ F>F MO( &A, O99S8T 0TA& S0& A6, J*< A6AT !Sa)e the character in A6 in the array 'OD8D. ! MO(8 '0;SO; TO ?8AT 6,?8 ! Display the array 'OD8D. ! exit to DOS 8?D

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3+ o) *+

E-&eri"ent I ; String Bandling Instructions


Introduction: ,n this experiment you will deal with string handling instructions, such as reading a string, mo)ing a string from one memory location to another, and comparing two strings. @ou will need some of the programs de)eloped in pre)ious experiments to rewrite them in a more structured way. Objectives: *+ More on Macros, Subroutine 'alls and Stac% operation. -+ String <andling ,nstructions. /+ ,ntroduction to the )ideo display. String Bandling Instructions: String handling instructions are )ery powerful because they allow the programmer to manipulate large bloc%s of data with relati)e ease. &loc% data manipulation occurs with the string instructions indicated in. 8ach of the string instructions indicated in define an operation for one element of a string only. Thus, these operations must be repeated to handle a string of more than one element. 9or repeating prefixes, see String handling instructions use the direction flag, S, and D, registers. The Direction 9lag (D9 selects auto+increment or auto+decrement operation for the D, and S, registers during string operations. $hene)er a string instruction transfers a byte, the contents of S, andKor D, increment or decrement by *. ,f a word is transferred, the contents of S, andKor D, increment or decrement by -. 1or"at C$D STD O&eration Mode E))ect 'lear D9! (D9 R Auto ,ncrement S, S, S *! D, D, S* Set D9! (D9 * Auto Decrement S, S, + *! D, D, +* Table ;! 7 Auto+ incrementing and decrementing in string instructions

T%e $ODS Instruction7

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3? o) *+

6ODS loads A6 or AA with data stored at the data+segment offset address indexed by the S, register. The 6ODS& causes a byte to be loaded into A6, and the 6ODS$ causes a word to be loaded into AA. T%e STOS Instruction7 The STOS instruction stores A6 or AA at the extra+segment memory location addressed by the D, register, (in fact 8S7D, . The STOS& stores a byte in A6 at the extra+segment memory indicated by D,. The STOS$ stores a word in AA at the extra+segment memory indicated by D,. "rogram O.* gi)es an example on the use of STOS instruction to clear the )ideo memory. T%e MO6S Instruction7 The MO(S instruction transfers data from one memory location to another. This is the only memory+to+ memory transfer allowed in the ,ntel family of Microprocessors. The MO(S instruction transfers a byte or a word from the data+segment location addressed by S, to the extra+segment location addressed by D,. The pointers then increment or decrement as indicated by the direction flag. Mne"onics $ODS STOS MO6S Meaning 6oad string Store string Mo)e string 1or"at $ODS5 $ODS4 STOS5 STOS4 MO6S5 MO6S4 O&eration As &er Direction 1lag (A6 or AA ((DS RS(S, (S, (S, * or ((8S R S (D, (A6 or AA (D, (D, * or ((8S R S (D, ((DS RS(S, (S, (S, * or (D, (D, * or 1lags a))ected ?one ?one ?one

0ote7 & stands for &yte and $ for $ord. Table ;! /7 &asic String <andling ,nstructions E-a"&le o) a "ove string: &elow is an example of the MO(S instruction. The same example is repeated later but with the use of the ;8" prefix. MO( AA, VDATA MO( DS, AA MO( 8S, AA 68A S,, &6=* 68A D,, &6=MO( 'A, ? '6D ?8AT7 MO(S& 6OO" ?8AT String Co"&arisons:

! Ma%e 8S D DS ! Source address for bloc%* ! Destination address for bloc%! ? D number of bytes to mo)e ! Set Auto+,ncrement mode ! Mo)e one byte at a time

,n order to allow a section of memory to be compared against a constant or another section of memory, the String Scan instruction S'AS is used. The S'AS instruction compares the content of the A6 register with a byte bloc% of memory, or the AA register with a word bloc% of memory. The opcode used for byte comparison is S'AS& and for word comparison is S'AS$ . The 'ompare Strings ,nstruction ('M"S compares two sections of memory data as bytes ('M"S& , or words ('M"S$ . The contents of the data+segment memory indicated by S, are compared with the contents of the data+segment memory indicated by D,. The 'M"S instruction increment both S, and D, if the direction flag (D9 is >ero, or decrements both of them if D9 is set to one.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3= o) *+

The 'M"S instruction is normally used with either the ;8"8 or ;8"?8 prefix. Alternates to these prefixes are ;8"L (repeat *&ile +ero and ;8"?L (repeat *&ile not +ero , though ;8"8 and ;8"?8 are more common . Mne"onics CMPS Meaning 'ompare strings 1or"at CMPS5 CMPS4 O&eration Set flags as per7 ((8S R S (S, Y ((8S RS(D, (S, (S, * or (D, (D, * or Set flags as per7 (A6 or AA Y ((8S RS(D, (D, (D, * or 1lags a))ected '9,"9,A9, L9,S9,O9

SCAS

Scan string

SCAS5 SCAS4

'9,"9,A9, L9,S9,O9

0ote7 & stands for &yte and $ for $ord. Table ;! 27 String 'ompare ,nstructions Re&eat Pre)i-es: Table O. 1 summari>es the repeat prefixes to be used with the string instructions gi)en in The ;8" prefix7 The ;8" prefix is added to any data transfer or compare instruction, except the 6ODS instruction. The ;8" prefix causes the 'A register to decrement by * each time the string instruction executes. ,f 'A reaches R, the instruction terminates and the program continues with the next se3uential instruction. The following example illustrates the of a mo)e string using the ;8" prefix7 MO( AA, VDATA MO( DS, AA MO( 8S, AA ! Ma%e 8S D DS '6D ! Set Auto+,ncrement mode MO( 'A, -R< MO( S,, O99S8T DATA* MO( D,, O99S8T DATA;8" MO(S& Used wit% Pre)iREP MO(S STOS 'M"S S'AS 'M"S S'AS Meaning

;epeat while not end of string 'A R REPE ;epeat while not end of string and strings are e3ual REPH 'A R and L9 D * REP0E ;epeat while not end of string REP0H And strings are not e3ual 'A R and L9 D R 0ote7 & stands for &yte and $ for $ord. Table ;! 3: "refixes fo use with basic string instructions E-a"&les on t%e use o) t%e SCAS and CMPS instructions7 The following example shows how to search a memory section of *RR bytes in length and starting at location &6O'=. The program searches if any location contains the )alue 12<.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page 3; o) *+

MO( D,, O99S8T &6O'= '6D MO( 'A, *RR MO( A6, 12< ;8"?8 S'AS&

!address data !auto+increment !load counter !A6 D 12< !search

The next example illustrates a short procedure that compares two sections of memory searching for a match. The 'M"S& instruction is prefixed with a ;8"8. This causes the search to continue as long as an e3ual condition exists. $hen the 'A register becomes R, or an une3ual condition exists, the 'M"S& instruction stops execution. After the 'M"S& instruction ends, the 'A register is >ero or the flags indicate an e3ual condition when the two strings match. ,f 'A is not >ero or the flags indicate a not+e3ual condition, the strings do not match. MAT'< ";O' 9A; MO( S,, O99S8T 6,?8 MO( D,, O99S8T TA&68 '6D MO( 'A, *R ;8"8 'M"S& ;8T 8?D"

MAT'< Pre $ab 4or,7 *. -. /. $ab 4or,: *+ -+

;ead the manual and understand how the string instructions wor%. $rite programs O.*, O.- and O./ and chec% their functionality. &ring your wor% to the lab.

Show programs O.*, O.- and O./ to your lab instructor. 'lear the screen using program O.- and write the word &0. somewhere on the display. ;un program O./ and chec% that it really detects the word &0. on the screen. 'lear the screen with program O.- and chec% again with program O./. /+ Modify program O./ so that it loo%s for the word MO( on the display, and counts the number of times it occurs. 'all it program O.1. 1+ 8dit one of your assembly language programs on the screen using the following7 T@"8 program.asm 2+ 'hec% with program O.1, how many times you ha)e the word MO( on the screen.

$ab Assign"ent: ;ewrite the program that reads a password without echo from the %eyboard in a more structured way, using Macros and "rocedures. To chec% for password )alidity use the string handling instructions 'M"S& or S'AS&. TIT$E @Progra" ;! A !This program clears the )ideo text display .MOD86 T,?@ .'OD8 .STA;T0" '6D MO( MO( MO( AA,R&ORR< 8S,AA D,,RRRR !select increment mode !address segment &ORR< !(ideo Text Memory D &ORR7RRRR !address offset RRRR Page 3* o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

MO( MO( ;8" .8A,T 8?D

'A,-2XOR AA,RN-R< STOS$

!load count7 -2 lines per OR columns !load data A<D RN< D color7 white text on blac% !bac%ground. A6 D -R< D space !clear the screen !exit to DOS !end of file

!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TIT$E @Progra" ;!/A !This program scrolls the display one line up .MOD86 T,?@ .'OD8 .STA;T0" '6D MO( MO( MO( MO( MO( MO( ;8" AA,R&ORR< 8S, AA DS, AA S,,*JR D,,R 'A,-1XOR MO(S$ !select T,?@ model !indicate start of 'OD8 segment !indicate start of program !select increment !load 8S and DS with &ORR

!address line *7 *JR D - X OR !address line R !load count !scroll screen !clear bottom line

MO( D,,-1XORXMO( 'A,OR MO( AA,RN-R< ;8" STOS$ .8A,T 8?D

!exit to DOS !end of file

MTIT$E @Progra" ;!2A !This program tests the )ideo display for the word &0. !if &0. appears anywhere on the display the program display @ !if &0. does not appear, the program displays ? ! .MOD86 SMA66 !select model SMA66 .DATA !indicate start of DATA segment DATA* D& F&0.F !define &0. .'OD8 !indicate start of 'OD8 segment .STA;T0" !indicate start of program MO( AA,R&ORR< !address segment &ORR with 8S MO( 8S,AA MO( 'A,-2XOR !set count '6D !select increment MO( D,,R !address first display position 6*7 MO( S,,O99S8T DATA* !address &0. "0S< D, !sa)e display address 'M"S& !test for & Z?8 6!if display is not & ,?' D, !address next display position 'M"S& !test for 0 Z?8 6!if display is not 0 Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page +< o) *+

,?' D, 'M"S& MO( D6,F@F Z8 6/ 6-7 "O" ADD 6OO" "0S< MO( 6/7 "O" MO( ,?T .8A,T 8?D D, A<,-*< D, D,,6* D, D6,F?F

!address next display position !test for . !load @ for possible &0. !if &0. is found !restore display address !point to next display position !repeat until entire screen is tested !sa)e display address !indicate ? if &0. not found !clear stac% !display D6 function !display AS',, from D6 !exit to DOS !end of file

E-&eri"ent I * Interru&ts
Introduction: On the ORxOJ, there are three types of e)ents commonly %nown as interrupts7 traps, exceptions and interrupts (hardware interrupts . ,n this experiment we will describe each of these forms and discuss their support on the ORxOJ '"0#s and "' compatible machines. $e will also describe ,nterrupt Ser)ice ;outines (,S; and Terminate and Stay ;esident (TS; programs. A TS; is a program that remains in memory after execution. The purpose of a TS; is to install an interrupt hoo%. This experiment illustrates the installation of TS; software using interrupt hoo%s and hot+%ey se3uences. ,n most cases, either an ,?T O cloc% tic%, or a hot+ %ey se3uence acti)ates a TS; program. Objectives: *+ -+ /+ 1+ ,nstall, use and uninstall an ,nterrupt Ser)ice ;outine (,S; . ,nstall+interrupt ser)ice procedures (hoo%s as TS; software. ,nstall a TS; interrupt hoo% that uses the cloc% tic% interrupt (,?T O . ,nstall a TS; interrupt hoo% that intercepts the %eyboard and responds to a particular %ey code or hot+%ey.

Interru&ts9 Tra&s9 and E-ce&tions: On the ORxOJ, there are three types of interrupts7 traps, exceptions and interrupts (hardware interrupts . ,n the following we will describe each of these forms and discuss their implementation. Although the terms trap and exception are often used synonymously, we will use the term trap to denote a programmer initiated and expected transfer of control to a special handler routine. Tra&s: Traps, or software interrupts, are speciali>ed subroutine calls in)o%ed by a software+ interrupt. The ORxOJ int instruction is the main instruction for executing a trap. Since traps execute )ia an explicit instruction, it is easy to determine exactly which instructions in a program will in)o%e a trap handling routine. There are two main differences between a trap and an arbitrary far procedure call7 the instruction used to call the

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page + o) *+

routine ('A66 )s. ,?T and the fact that a trap pushes the flags on the stac% so you the ,;8T instruction must be used to return from it. The main purpose of a trap is to pro)ide a fixed subroutine that )arious programs can call without ha)ing to actually %now the run+time address. The ,?T -*h instruction is an example of a trap in)ocation. "rograms do not ha)e to %now the actual memory address of DOS entry point to call DOS. ,nstead, DOS patches the interrupt -*h )ector when it loads into memory. $hen an ,?T -*h is executed, the ORxOJ automatically transfer control to DOS entry point. Traps are used to call a resident program function. &y patching an interrupt )ector to point at a subroutine within the resident code, other programs that run after the resident program terminates can call the resident subroutines by executing the appropriate ,?T instruction. Most resident programs do not use a separate interrupt )ector entry for each function they pro)ide. ,nstead, they usually use a single interrupt )ector and transfer control to an appropriate routine through a function number that the caller passes in a register, usually and con)entionally the A< register. A typical trap handler would execute a case statement on the )alue in the A< register and transfer control to the appropriate handler function. E-ce&tions: An exception is an automatically generated trap, forced rather than re3uested, that occurs in response to some exceptional condition. .enerally, there isnFt a specific instruction associated with an exception, instead, an exception occurs in response to some erroneous beha)ior of normal program execution. 8xamples of conditions that may cause an exception include executing a di)ision instruction with a >ero di)isor, executing an illegal opcode, and a memory protection fault. $hene)er such a condition occurs, the '"0 immediately suspends execution of the current instruction and transfers control to an exception handler routine. This routine can decide how to handle the exceptional condition! it can attempt to rectify the problem or abort the program and print an appropriate error message. 8xceptions occur when an abnormal condition occurs during execution. There are fewer than eight possible exceptions on machines running in real mode. "rotected mode execution pro)ides many others. Although exception handlers are user defined, the ORxOJ hardware defines the exceptions that can occur. The ORxOJ also assigns a fixed interrupt number to each of the exceptions. Table P. * describes each of these exceptions in detail. ,n general, an exception handler preser)es all registers. <owe)er, there are se)eral special cases where you may want to twea% a register )alue before returning. ?e)ertheless, you should not arbitrarily modify registers in an exception handling routine unless you intend to immediately abort the execution of your program.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page +/ o) *+

I0T I I0T <

E-ce&tion 1unction Di)ide 8rror

Descri&tion Occurs whene)er an attempt to di)ide a )alue by >ero or the 3uotient does not fit in the destination register when using the div or IDI6 instructions. The 9"0Fs 9D,( and 9D,(; instructions do not raise this exception. Occurs after e)ery instruction if the trace bit in the flag register is set. Debuggers will often set this flag to trace the execution of a program. This exception is actually a trap, not an exception. ,t occurs when the '"0 executes an I0T2 instruction. <owe)er, it is considered as an exception since programmers rarely put I0T 2 instructions directly into their programs. ,nstead, a debugger li%e 'ode(iew often manages the placement and remo)al of I0T 2 instructions. 6i%e I0T 29 this exception is technically a trap. ,t is raised when an I0TO instruction is executed and the o)erflow flag is set. ,f the o)erflow flag is clear, the I0TO instruction is a 0OP! ,f t%e over)low )lag is set9 I0TO beha)es li%e an I0T 3 instruction! An I0TO instruction can be inserted after an integer computation to chec% for an arithmetic o)erflow. The OR-OJ and later processors raise this exception if an attempt to execute an opcode that does not correspond to a legal ORxOJ instruction is made. These processors also raise this exception if you attempt to execute a bound, 6DS, 68S, 6,DT, or other instruction that re3uires a memory operand but you specify a register operand in the modKrm field of the modKregKrm byte. The OR-OJ and later processors raise this exception if an 9"0 (or other coprocessor instruction is attempted to execute without ha)ing the coprocessor installed. This exception can be used to simulate the coprocessor in software. Table *! 7 8xceptions

I0T

Single Step (Trace

I0T 2

&rea%point

I0T 3 I0TO

O)erflow

I0T ?

,n)alid Opcode

I0T =

'oprocessor ?ot A)ailable

Bardware interru&ts7 <ardware interrupts, or simply interrupts, are program control interruptions based on an external hardware e)ent (external to the '"0 . These interrupts generally ha)e nothing at all to do with the instructions currently executing! instead, some e)ent, such as pressing a %ey on the %eyboard or a time out on a timer chip, informs the '"0 that a de)ice needs some attention. The '"0 interrupts the currently executing program, ser)ices the de)ice, and then returns control bac% to the program. On the "', interrupts come from many different sources. The primary sources of interrupts, howe)er, are the timer chip, %eyboard, serial ports, parallel ports, dis% dri)es, 'MOS real+time cloc%, mouse, sound cards, and other peripheral de)ices. These de)ices connect to an ,ntel O-2PA programmable interrupt controller (",' that prioriti>es the interrupts and interfaces with the ORxOJ '"0. The O-2PA chip adds considerable complexity to the software that processes interrupts, so it ma%es perfect sense to discuss the ",' first, before trying to describe how the interrupt ser)ice routines ha)e to deal with it. Afterwards, this section will briefly describe each de)ice and the conditions under which it interrupts the '"0. This text will fully describe many of these de)ices in later chapters, so this chapter will not go into a lot of detail except when discussing the timer interrupt.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page +2 o) *+

;<-;? Interru&t Structure and Interru&t Service Routines: An interrupt ser)ice routine is a procedure written specifically to handle a trap, exception, or interrupt. Although different phenomenon cause traps, exceptions, and interrupts, the structure of an interrupt ser)ice routine, or ,S;, is approximately the same for each of these. The ORxOJ allow up to -2J )ectored interrupts. This means that up to -2J different sources can exist for an interrupt and the ORxOJ will directly call the ser)ice routine for that interrupt without any software processing. ?on+)ectored interrupts transfer control directly to a single interrupt ser)ice routine, regardless of the interrupt source. The ORxOJ pro)ides a -2J entry interrupt )ector table beginning at address RRRR7RRRR in memory. This is a *=byte table containing -2J 1+byte entries. 8ach entry in this table contains a segmented address that points at the interrupt ser)ice routine in memory. .enerally, we will refer to interrupts by their index into this table, so the address ()ector of interrupt n is at memory location RRRR7nX1. ,nterrupt >eroFs )ector is at address RRRR7RRRR, interrupt oneFs )ector is at address RRRR7RRR1, etc. $hen an interrupt occurs, regardless of its source, the ORxOJ does the following7 *. -. The '"0 pushes the flags register onto the stac%. The '"0 pushes a far return address (segment7offset onto the stac%, segment )alue first. The '"0 determines the cause of the interrupt, i.e., the interrupt number, and fetches the four byte interrupt )ector from address RRRR7)ectorX1. The '"0 transfers control to the routine specified by the interrupt )ector table entry.

/.
1.

After completion of these steps, the ,S; ta%es control. $hen the ,S; wants to return control, it must execute an ,;8T (interrupt return instruction. The interrupt return pops the far return address and the flags off the stac%. ?ote that executing a far return is insufficient since that would lea)e the flags on the stac%. <ardware interrupts are processed differently than other types of interrupts. 0pon entry into the hardware ,S;, the ORxOJ disables further hardware interrupts by clearing the interrupt flag. Traps and exceptions do not do this. ,f further hardware interrupts are to be disabled within a trap or exception handler, one must explicitly clear the interrupt flag with a clear interrupt flag instruction ('6, . 'on)ersely, if interrupts are to be enabled within a hardware ,S;, one must explicitly turn them bac% on with a Set ,nterrupt instruction (ST, . ?ote that the ORxOJFs interrupt disable flag only affects hardware interrupts. 'learing the interrupt flag will not pre)ent the execution of a trap or an exception.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page +3 o) *+

,S;s are written li%e almost any other assembly language procedure except that they return with an ,;8T instruction rather than ret. Although the distance of the ,S; procedure (near )s. far is usually of no significance, you should ma%e all ,S;s far procedures. This will ma%e programming easier if you decide to call an ,S; directly rather than using the normal interrupt handling mechanism. 8xceptions and hardware interrupts ,S;s ha)e a )ery special restriction7 they must preser)e all registers they modify. DOS Me"or# Usage and TSRs

1igure *! 7 DOS Memory with no acti)e application $hen DOS is first booted, the memory layout will loo% something li%e. DOS maintains a free memory pointer that points to the beginning of the bloc% of free memory. $hen an application program is run, DOS loads this application starting at the address the free memory pointer contains. Since DOS runs only a single application at a time, all the memory, starting from the free memory pointer to the end of ;AM (R&9999h , is a)ailable for the application#s use. $hen the program terminates normally, )ia DOS function 1'<, MS+DOS reclaims the memory in use by the application and resets the free memory pointer to :ust abo)e DOS in low memory.

1igure *! /: DOS Memory with no acti)e application

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ++ o) *+

MS+DOS pro)ides a second termination call which is identical to the terminate call e+ except that it does not reset the free memory pointer to reclaim all the memory in use by the application. ,nstead, this terminate+and+stay+resident call frees all but a specified bloc% of memory. The TS; call (A< D /*< re3uires two parameters, a process termination code in the A6 register (usually >ero and DA must contain the si>e of the memory bloc% in paragraphs to protect. $hen DOS executes this code, it ad:usts the free memory pointer so that it points at a location DAX*J bytes abo)e the program#s "S". This lea)es memory loo%ing li%e this7 1igure *! 27 DOS Memory Organi>ation for a ;esident "rogram

$hen the user executes a new application, DOS loads it into memory at the new free memory pointer address, protecting the resident program in memory7 1igure *! 37 DOS Memory with a ;esident Application 4riting and Installing a TSR &rogra": A TS; program consists of two parts an installation section and a ser)ice routine. The ser)ice routine may consist of more than one ,S;. The installation section is executed only at load time. The ,S; is executed each time the interrupt is in)o%ed. An ,S; must end with a ,;8T instruction or a 9A; ZM" to another ,S;. *. ,nstalling an ,S;7 The installation section prepares the TS; program#s ser)ice routine to be used by other programs or to ser)ice a hardware interrupt. The installation uses ,?T -*< 9unction -2<. The following code is a typical installation section7 MO( AA, 'S MO( DS, AA MO( DA, Offset "gmW,S; MO( A<, -2< MO( A6, ,ntW?umber ,?T -*< - K Ma%e an ,S; a TS;7

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page +? o) *+

This procedure uses ,?T -*< 9unction /*<. DA contains the number of paragraphs to be %ept in memory. The following code ma%es the installed ,S; a TS; routine. MO( DA, ?umberWOfW"aragraphs MO( A6, ;eturnW'ode MO( A<, /*< ,?T -*< 1unction /+B 2 B 2+B 0otes7 * + The code for an installation routine of a TS; should come after the ser)ice section, to ensure that it is freed by ,?T -*< 9unction /*<, which return control to DOS. Therefore, such a procedure does not need to ha)e a ;8T instruction. - + The number of paragraphs to be %ept resident in memory is found by di)iding the number of bytes to be %ept resident by *J. The following code performs such an operation7 MO( DA, Offset ,nstallationW;outine MO( '6, R1 S<; DA, '6 ! Di)ide by *J E))ect Set ,nterrupt (ector Ma%e ,S; as TS; .et ,nterrupt (ector for a Specified ,nterrupt. In&ut DS7DA D Segment Offset Address DAD number of paragraphs to be %ept in memory A6 D ,nterrupt number Out&ut

8S7&A D Segment Offset Address Table *! /: ,S; 9unction Manipulation

Installation_Routine ENDP

. . . . .
Installation_Routine PROC

Service Section

Nu !er_O"_Para#ra$%s & O""set Installation_Routine' 16

CS

,?' DA

! to ta%e care of truncation if any

1igure *! +: Memory Structure after a TS; program is installed

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page += o) *+

User De)ined TSRCs: The software )ectors from JR< through JN< and those from R9*< through R99< in the ,nterrupt (ector Table are undefined and a)ailable for user defined ,S;s. "rogram P.* installs an ,S; at ,?T JR<. 9rom the time the ,S; is installed until either the system is powered down or ,?TJR< is re+)ectored to address some other routine, any program that contains the se3uence7 MO( AA, Operand ,?T JR< will display the message 4$elcome To The ,nteresting $orld Of TS; ;outinesF. ,f we change the body of program P.*, in the following way7 D8',MA6WD,S"6A@ ";O' 9A; MO( S,, RRRR MO( &A, *R MO( AA,operand D,( &A MO( 'S7A;;A@BS,C, D6 ,?' S, 'M" AA, RRRR Z?L D,(W6OO" D8' S, MO( A<, R-< MO( D6, 'S7A;;A@BS,C O; D6, /R< ,?T -*< 'M" S,, RR Z?L D,S"6A@W6OO" "O" S, "O" &A "O" AA ,;8T

D,(W6OO"7

D,S"6A@W6OO"7

D8',MA6WD,S"6A@

8?D"

,n this case a call to ,?T JR< will display the contents of AA register (operand on the console screen in decimal notation. Another program, which displays a number passed through DA in different numbering systems will be gi)en in the lab. The numbering system is decided by the )alue gi)en in the A< register. Redi)inition o) an e-isting ISR: An existing system ,S; can be redefined by replacing its )ector by the )ector of a user defined ,S;. ,f execution is to resume at the redefined ,S;, DOS function /2< is used to retrie)e the segment7Offset address of the interrupt to be redefined. This address is then stored in a double+word )ariable ((A; . The address is then used by the new ,S; to return control to the redefined ,S; by executing the 9A; :ump7 GMP CS:6AR ,f control is not to resume at the redefined ,S;, the new ,S; ends with an ,;8T. I0T / B 1unction /+B returns the /-+bit )ector address of a specified interrupt )ector in 8S7&A . The following code retrie)es the )ector address of ,?T RP<7 MO( A<, /2< Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing !.et ,nt. (ector Page +; o) *+

MO( A6, RP< ,?T -*< MO( $O;D "T; (A;,&A MO( $O;D "T; (A;S-,&A

!9or ,?T RP< !Store Offset Address !Store Segment ?umber

$hen an existing system interrupt is being redefined, it is better to disable mas%able interrupts. <ardware interrupts can occur at any time. 9or this reason, interrupts should be disabled while ma%ing changes in the )ector table, in order to a)oid the ris% of an interrupt ta%ing place while there is no )alid address for a ser)ice routine. The '6, and ST, instructions are used for this purpose. A typical installation routine is gi)en in program P./. ,nstruction '6, ST, Meaning 9lags Affected 'lear ,nterrupt 9lag! Disable Mas%able ,9 D R ,nterrupts Set ,nterrupt 9lag! ;e+enable Mas%able ,9 D * ,nterrupts Table *! 2: ,nterrupt 9lag Set and 'lear ,nstructions

T#&ical Case9 Interce&tion o) t%e 8e#board Interru&t: T%e ,e#board &ort: $hen a %ey is pressed, the %eyboard controller sends an O+bit scan code to to port JR<. The %ey stro%e triggers a hardware interrupt, which prompts the '"0 to cal ,?T RP<. This interrupt inputs then the scan code from the port. T%e DOS ,e#board status )lag: The %eyboard status flag is located at RR1R7RR*N<. This status flag can be obtained either by7 O Using 5IOS I0T ?B9 )unction </B: MO( A<, R-< ,?T *J< !Status flag returned in A6 register or7 / O Directl# reading seg"ent 3<B: MO( AA, RR1R< MO( 8S, AA MO( D,, RR*N< MO( A6, 8S7BD,C To test for the status of a bit use the T8ST instruction. T8ST A6, RR*RRRRR& ! Test ?um 6oc% N J 2 1 / * R

&it R7 ;ight Shift =ey Down &it *7 6eft Shift =ey Down &it -7 'trl =ey Down &it /7 Alt =ey Down

&it 17,nsert On &it 27 'aps 6oc% On &it J7 num 6oc% On &it N7 Scrol 6oc% On

1igure *! ?: =eyboard Status ;egister "rogram P.1 is a TS; that beeps the spea%er once when the numeric %eypad is used, pro)ided that the ?um 6oc% is on. ?ote that the scan codes of the %eys in the numeric %eypad area are from N* to O/. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page +* o) *+

I"&ortant 0ote: All ,S; or TS; programs are 'OM (not 8A8 files. To generate a 'OM file, assemble your program either using the option7 assemble as 'OM in the "$& en)ironment, or using the command7 M6 Kat. Pre $ab 4or,7 *+ -+ /+ 1+ 2+ $rite, assemble lin% and run program P.*. 0se the command M6 KAT to generate a 'OM file. $rite, assemble lin% and run program P.-. 'hange "rogram P.* using the code gi)en to display a number in AA in decimal format. $rite a program that displays a number in decimal format using ,?T JR<. &ring your wor% to the lab.

$ab 4or,: *+ Show programs P.* and P.- to your lab instructor. -+ $rite a "rogram that chec%s for the )alidity of your password, and beeps the spea%er once if the password is right, and twice if not. .uidelines7 - 9irst use the program already de)eloped in pre)ious experiment to chec% for password )alidity. Second use macros as much as you can. - Third ma%e the password chec%ing routine as a TS; program. 'all it ,?T JR<. $hen in)o%ed this procedure chec%s for password )alidity and returns A6 D RR< if the password is correct, and A6 D R99< is the password is incorrect.

TIT$E PProgra" *! Q !This program installs an ,S; at ,?T JR<. $hene)er in)o%ed, as in! "rogram P.-, ,?T JR< displays the message F$elcome To The !,nteresting $orld Of TS; ;outinesF .MOD86 T,?@ .'OD8 O;. *RR< .STA;T0" ZM" ,?STA66 ,S;WJR< ";O' 9A; "0S< AA "0S< DA "0S< DS MO( AA, 'S MO( DS, AA MO( DA, O99S8T MS. MO( A<, RP< ,?T -*< "O" DS "O" DA "O" AA ,;8T ,S;WJR< 8?D"

!9orce code at Offset *RR<

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ?< o) *+

MS. D& F$elcome To The ,nteresting $orld Of TS; ;outinesF,RD<,RA<,FUF ,?STA66 ";O' MO( AA, 'S MO( DS, AA MO( A<, -2< MO( A6, JR< MO( DA, O99S8T ,S;WJR< ,?T -*< MO( A<, /*< MO( A6, RR< MO( DA, O99S8T ,?STA66 MO( '6, R1 S<; DA, '6 ,?' DA ,?T -*< ,?STA66 8?D" 8?D TIT$E PProgra" *!/Q ! This program uses the newly installed ,?T JR< .MOD86 T,?@ .'OD8 .STA;T0" MO( 'A, 2 ! 6oop 2 times 6*7 ,?T JR< ! Display the message 6OO" 6* .8A,T 8?D TIT$E PProgra" *!2Q !The following code redefines an existing ,S; (A; DD M !0sed to store the Old ,nterrupt (ector ... ,?STA66AT,O?W;O0T,?8 ";O' '6, !'lear ,9 to pre)ent <ardware ,nterrupt !.8T O6D ,?T8;;0"T (8'TO; MO( A<, /2< MO( A6, ,?T8;;0"TW?0M&8; ,?T -*< !SA(8 T<8 ,?T8;;0"T (8'TO; MO( $O;D "T; (A;,&A !Store Offset Address MO( $O;D "T; (A;S-,8S !Store Segment ?umber !,?STA66 ?8$ ,?T8;;0"T (8'TO; MO( AA, 'S MO( DS, AA MO( A<, -2< MO( A6, ,?T8;;0"TW?0M&8; MO( DA, O99S8T ?8$W,S; Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page ? o) *+

,?T -*< !T8;M,?AT8 A?D STA@ ;8S,D8?T MO( A<, /*< MO( A6, RR< ST, ! Set ,9 to enable <ardware ,nterrupt ,?T -*< ,?STA66AT,O?W;O0T,?8 8?D"

TIT$E PProgra" *!3Q .MOD86 T,?@ .'OD8 O;. *RR< !9orce code at Offset *RR< .STA;T0" ZM" ,?STA66 ?8$WRP<W,S; ";O' 9A; "0S<9 "0S< AA "0S< 8S "0S< D, "0S< DA !"oint 8S7D, to the %eyboard flag byte MO( AA, 1R< MO( 8S, AA MO( D,, *Nh MO( A6, 8S7BD,C T8ST A6, RR*RRRRR& !?0M 6O'= STAT8 M ZL 6AST ,? A6, JR< 'M" A6, N*< Z6 6AST 'M" A6, O/< Z. 6AST MO( A<, R-< MO( D6, RN< ,?T -*< 6AST7 "O" D, "O" 8S "O" AA "O"9 ,;8T ZM" 'S7O6DWRP<W(8'TO; ?8$WRP<W,S; 8?D" O6DWRP<W(8'TO; DD M ,?STA66 ";O' MO( A<, /2< MO( A6, RP< ,?T -*< !SA(8 O6D (8'TO; Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page ?/ o) *+

MO( $O;D "T; O6DWRP<W(8'TO;, &A MO( $O;D "T; O6DWRP<W(8'TO; S -, 8S !,?STA66 ?8$ ,?T. (8'TO; MO( AA, 'S MO( DS, AA MO( A<, -2< MO( A6, RP< MO( DA, O99S8T ?8$WRP<W,S; ,?T -*< MO( AA, /*RR< MO( DA, O99S8T ,?STA66 MO( '6, R1 S<; DA, '6 ,?' DA ,?T -*< ,?STA66 8?D" 8?D

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ?2 o) *+

E-&eri"ent I < Accessing 6ideo Me"or#


Introduction: This experiment introduces the use of the (.A controller and &,OS ,?T *R< functions to access )ideo memory using mode *-< graphics mode. @ou will be pro)ided with some routines that use the )ideo modes. These routines can be inserted into your programs. Objectives: *+ 0se the J1Rx1OR *J+color graphics display mode. -+ 0se mode *-< to di)ide the screen into a 2/ line by OR character per line to display bloc%s of colors. /+ Display text on the J1Rx1OR *J+color graphics display without changing the bac%ground color. Te-t Mode: ,n DOS mode, the )ideo text memory is located at &ORR7RRRR through &ORR79999 and contains AS',, data and attributes for display. ,n text mode, the following functions are used to display data on the screen. 1unction </B: Displays one character! May be interrupted by a 'trl &rea% 1unction <?B: May not be interrupted by a 'trl &rea% 1unction <*B: 0sed to display a character string terminated by a U sign. :ra&%ics Mode: The J1Rx1OR *J+color graphics display mode uses memory location ARRR7RRRR through ARRR79999 to access graphics data. ,n order to display *J colors with a resolution of J1Rx 1OR a memory greater than J1= bytes is re3uired. &ecause *J colors re3uire 1 bits, and the resolution is J1R x 1OR (i.e. /RN,-RR pixels , the memory system re3uires J1R x 1OR x 1 (i.e. *,--O,ORR bits or *2/,JRR bytes of )ideo memory in this display mode. To allow access to such as amount of memory, mode *-< display is designed to be accessed in bit planes. A bit plane is a linear section of memory that contains one of the four bits to display the *J colors. 8ach bit plane re3uires /RN,-RR bits of memory, stored in /O,1RR bytes of memory. The J1= bytes at segment ARRR< are enough to only address a single bit plane at a time. The bit plane is addressed at memory locations ARRR7RRRR through ARRR7P299. ,n a J1Rx1OR display, location ARRR7RRRR represents the upper leftmost O pixels, and location ARRR7P299 represents the lower rightmost O pixels. There are four planes, or ban%s of memory, that o)erlap this address range to represent the four bits or color for each pixel ( 9igure *R. . To change the color of one pixel, on the )ideo display, four bits need to be changed, one in each bit plane. The color codes used for a standard (.A display are shown in Table *R.-. ,f all 1 bit planes are cleared, blac% is the pixel color.

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page =+ o) *+

P0

P1

P2

P3

P0

P1 Plane 0

P0

P1 Plane 1

P1

P1 Plane 2

Plane 3

1igure <! 7 The four bit+planes of the J1Rx1OR, *J+color (.A display Accessing t%e 6ideo Me"or#7 Access to )ideo memory in mode *-< is accomplished through the following steps7 Ste& 7 ;ead the byte of memory to be changed, to load the bit plane information into the )ideo card. Ste& /7 Select and address a single pixel (bit through the graphics address register (.A; and bit mas% register (&M; . This is accomplished by sending an O out to ,KO port R/'8<, which represents the .A;. Steps * and - are done through the following set of instructions7 MO( DA, R/'8< MO( A6, RO O0T DA, A6 ! Select (.A address card ! ,ndex of O ! Select ,ndex O

Ste& 27 6oad A6 with the bits to be changed (a one bit represents a pixel to be changed , and send this out to the &it Mas% ;egister (&M; , or ,KO port R/'9<. MO( DA, R/'9< MO( A6, OR< O0T DA, A6 ! Select &M; ! "lace mas% in A6 ! Select leftmost bit, in this case ! using OR<.

Ste& 37 Set all mas% bits to *#s (**** or R9< in the Map Mas% ;egister (MM; at se3uencer offset -, and write color R to the (.A card (blac% to the address containing the pixel, to clear the old color from the pixel. Mas% bits select the bit planes to be changed. ,f all are selected and a color R is written, all four+bit planes are cleared to >ero. To do so, use the following code7 MO( DA, R/'1< MO( A6, RO0T DA, A6 MO( DA, R/'2< MO( A6, R9< O0T DA, A6 ! Select (.A se3uencer register ! ,ndex of ! Select ,ndex ! Address MM; ! Mas% to **** binary

Ste& +7 Send the desired color number to the MM; and write an 99< to the )ideo memory. This places a logic one in only the selected bit planes. To write a new color to a pixel on the screen, use the following instructions7 MO( A6, 'olor ! 'hoose color! e.g. R/ for cyan O0T DA, A6 ! Select color ! ?ext write an 99< to the selected )ideo memory location

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page =? o) *+

Register :AR 5MR MMR

Meaning .raphics Address ;egister &it Mas% ;egister Map Mas% ;egister Table <! : ;egisters used in (ideo Mode

Address R/'8< R/'9< R/'1< to access R/'2< to select bit planes

bl

&

br

& 'haracter &ac%ground &lin%ing

br 7 &rightness 1igure <! /7 The &it "attern A)ailable to (.A, Mode *-<

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page == o) *+

br R R R R R R R R * * * * * * * *

Code R : R R R R R * R * * R * R * * * * R R R R R * R * * R * R * * * *

5 R * R * R * R * R * R * R * R *

Color &lac% &lue .reen 'yan ;ed Magenta &rown $hite .rey &right &lue &right .reen &right 'yan &right ;ed &right Magenta @ellow &right $hite

Table <!/7 'olors A)ailable to (.A, Mode *-< DIRECT 6IDEO ACCESS I0 TE7T MODE The characters seen on the )ideo monitor correspond directly to AS',, bytes stored in the )ideo ;AM. Thus to display a character, by direct )ideo access, one need only place the AS',, code for that character into the correct )ideo ;AM location. E-a"&le7 The following program fills a screen with A#s by direct )ideo access. ,t uses the default text mode / ,STA'= -RR .'OD8 .STA;T0" MO( AA , R&ORR< MO( DS , AA MO( 'A , -RRR MO( D, , R MO( $O;D "T; BD,C , NR1*h ADD D, , 6OO" 9,66W"A.8 MO( A< , RO< ,?T -*< ! -RRR words ! blac% A on white ! wait for a %eystro%e

9,66W"A.87

.8A,T 8?D

The formula for calculating a )ideo memory offset address, in )ideo page R, gi)en a screen row and column coordinate pair is7 C%aracter o))set R J rowI S ;< T colu"nI K S / R J rowI S J?3 T ?K T colu"nI K S / 0sing the abo)e formula the following procedure calculates an OR X -2 text+mode memory address from a pair of row and column coordinates, contained in D< and D6 respecti)ely7 'A6'WADD;8SS ";O' Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page =; o) *+

! in&ut: D< D row number (R + -1 , D6 D column number (R + NP , (,D8OWS8. a constant which contains ! either &RRR< or &ORR< ! out&ut: 8S7D, contains the re3uired segment 7 offset address "0S< AA MO( AA , (,D8OWS8. MO( 8S , AA MO( A< , R MO( A6 , D< S<6 AA , * S<6 AA , * S<6 AA , * S<6 AA , * MO( D, , AA S<6 AA , * S<6 AA , * ADD D, , AA MO( A< , R MO( A6 , D6 ADD D, , AA S<6 D, , * "O" AA ;8T 'A6'WADD;8SS 8?D"

! AA 7D rowQ ! AA 7D rowQ X ! AA 7D rowQ X 1 ! AA 7D rowQ X O ! AA 7D rowQ X *J ! D, 7D rowQ X *J ! AA 7D rowQ X /! AA 7D rowQ X J1 ! D, 7D rowQ X OR ! AA 7D columnQ ! D, 7D rowQ X OR S columnQ ! D, 7D ( rowQ X OR S columnQ X -

Thus, for example, to display a yellow blin%ing T on a green bac%ground at row J and column /N, by direct )ideo access, use 7 MO( D< , J MO( D6 , /N 'A66 'A6'WADD;8SS MO( A< , *R*R***R& MO( A6 , TT# STOS$ ?ote7 The effect of STOS$ is7 MO( 8S7BD,C , A6 MO( 8S7BD, S *C , A< Using 5IOS I0T <B to access t%e video dis&la#7 Another way of accessing )ideo memory is through ,?T *R<. This method is recommended for most applications, since it frees the user from the burden of calculating )ideo memory addresses. The following are most functions used with ,?T *R<, these allow most useful )ideo tas%s. ?ote that ,?T *R< preser)es only the &A, 'A, DA, and the segment registers Accessing t%e 6ideo Me"or#: ?ote that color codes are arranged so that the leftmost bit represents bright, and the next three bits represent red, blue and green respecti)ely. Access to the )ideo memory is explained in the following sections. &efore accessing )ideo, ma%e sure that you sa)e the current )ideo mode so that you can restore it once you finish your program. This can be done using the following se3uence of instructions7 ( ,?T *R< MO( A<,R9< ,?T *R< "0S< AA !.et current )ideo mode !Sa)e (ideo mode A6 and ?umber of columns A< Page =* o) *+ ! rowQJ ! columnQ/N ! attribute7 yellow on green

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

]]]] "O" AA MO( A<,RR ,?T *R< Select 6ideo Mode:

!;estore (ideo mode A6 and ?umber of columns A<

MO( A<, RR MO( A6, (,D8OWMOD8 ,?T *R< 9unction RR automatically clears the screen. To preser)e the screen while changing the mode set the most significant bit of A6 to *. MO( MO( O; ,?T A<, RR A6, (,D8OWMOD8 A6, OR< *R<

:et Current 6ideo Mode: MO( ,?T "0S< A<, R9< *R< AA

! Or MO( OldW(ideoWMode, AA

Restore 6ideo Mode: "O" MO( ,?T AA A<, RR< *R< ! Or MO( AA, OldW(ideoWMode

Cursor Positioning: ,f the row and column numbers are in <exadecimal they can directly be assigned to the DA register. The cursor positioning on a )ideo page is independent of the other )ideo pages.

Set Cursor Position: MO( A<, R-< MO( &<, 'urrentW(ideoW"ageW?umber !0sually R MO( D<, ;owW?umber MO( D6, 'olumnW?umber ,?T *R< :et Cursor Position: MO( A<, R/< MO( &<, 'urrentW(ideoW"ageW?umber !0sually R ,?T *R< MO( Sa)eW'ursor, 'A MO( 'urrentW;ow, D< MO( 'urrentW 'olumn, D6 Set Cursor Si'e: The cursor is displayed using starting and ending scan lines. ,n Mono mode the cursor uses *- lines (R,*,-, .. R&<,R'< , whereas in color mode it uses O lines (R,*, ..,J,N .

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ;< o) *+

'.AK8.A R * / 1 2 J N R * / 1 2 J N O P RA R& R'

MO?O'<;OM8

default

default

1igure *! 27 'ursor Si>e MO( MO( MO( ,?T A<, R*< '<, StartWScanW6ineQ '6, 8ndWScanW6ineQ *R<

To set the cursor to its maximum si>e in color mode7 MO( MO( ,?T A<, R*< 'A, RRRN< *R<

To set the cursor to its maximum si>e in monochrome mode7 MO( MO( ,?T 4rite Pi-el: MO( ,?T A<, R'< *R< A<, R*< 'A, RRR'< *R<

Save t%e current cursor si'e: MO( 'ursorWSi>e, 'A

Restore t%e current cursor si'e: MO( MO( ,?T A<, R*< 'A, 'ursorWSi>e *R<

Ma,e t%e Cursor Invisible: Set the starting scan line to an illegal )alue by setting bit 2 in '< to *. MO( O; ,?T A<, R*< '<, RR*RRRRR& *R< ! Or MO( 'A, -RRR<

Another way of hiding the cursor is to place it in the undisplayed portion of the )ideo page, e.g. row Q-2 column Q R. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page ; o) *+

MO( MO( MO( MO( ,?T

D<, -2 D6, RR A<, R-< &<, RR *R<

!;ow number !'olumn number !(ideo page Q R

Set 5order Color: MO( MO( MO( ,?T Pre $ab 4or,7 A<,R&< &<,RR< &6,R1< *R<

*+ $rite two Macros7 one to get the current )ideo mode, and the other restore the )ideo mode. -+ $rite and run programs *R.* and *R.-. $rite your programs using macros and procedures. /+ "repare all programs in this experiment by writing them using macros and procedures. $ab 4or,: *+ Show programs *R.* and *R.- to your lab instructor. -+ $rite and run programs *R./ and *R.1. /+ $rite a program that displays the time on the top right hand corner of the display. 0se ,?T *R< function R-, which inputs the column and row numbers in D6 and D< respecti)ely, and (ideo page (usually R in &<. $ab Assign"ent: ;ewrite the program that displays the time on the screen using graphics mode only. ;e)iew the part that shows how to display text in )ideo mode. Title T"rogram *R+*# ! A program that blan%s the test mode screen and ma%es it red. ! ,t then displays the message This is a test line. before ! returning to DOS. ! .MOD86 SMA66 .DATA M8S D& FThis is a test line.UF .'OD8 .STA;T0" MO( AA,R&ORR< !address text segment MO( 8S,AA '6D !select increment MO( D,,R !address text offset MO( A<,1R< !attribute blac% on red MO( A6,-R< !character is space MO( 'A,-2XOR !set count ;8" STOS$ !clear screen and change attributes MO( MO( MO( ,?T A<,&<,R DA,R *R< !home cursor !page R !row R, char R

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ;/ o) *+

MO( MO( ,?T .8A,T 8?D

DA,O99S8T M8S A<,P -*<

!display GThis is a test line.G

Title T"rogram *R+-# !a program that displays all of -2J colors a)ailable to the !/-R x -RR )ideo display mode (*/< !XXXusesXXX !the &A?D procedure to display J1 colors at a time in a band !on the display. ! .MOD86 T,?@ .'OD8 .STA;T0" MO( AA,*/< !select mode */< ,?T *R< MO( MO( '6D MO( AA,RARRR< 8S,AA D,,R !address segment ARRR with 8S !select increment !address offset RRRR !load starting test color of RR< !display one band of J1 colors !load starting color of 1R< !display one band of J1 colors !load starting color of OR< !display one band of J1 colors !load starting color of 'R< !display one band of J1 colors !wait for any %ey !switch bac% to DOS )ideo mode

MO( A6,R 'A66 &A?D MO( A6,J1 'A66 &A?D MO( A6,*-O 'A66 &A?D MO( A6,*P'A66 &A?D MO( ,?T MO( ,?T .8A,T A<,* -*< AA,/ *R<

! !the &A?D procedure displays a color band of J1 colors !XXXinput parameterXXX !A6 D starting color number !8S D ARRR< !D, D starting offset address for display ! &A?D ";O' ?8A; MO( &A?D*7 "0S< MO( &A?D-7 MO( &A?D/7 STOS& D8' &<,1R AA 'A,J1 &6,2 &6 Page ;2 o) *+ !load line count !sa)e starting color !load color across line count !load times color is displayed !store color

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Z?L ,?' 6OO" "O" D8' Z?L ADD ;8T &A?D 8?D" 8?D

&A?D/ A6 &A?DAA &< &A?D* D,,/-RX*R

!repeat 2 times !change to next color !repeat for J1 colors !restore starting color !repeat for 1R lines !s%ip *R lines

Title T"rogram *R+/# !a program that displays all the possible brightness le)els of the !color red for the /-R x -RR, -2J color mode (*/< ! .MOD86 T,?@ .'OD8 .STA;T0" MO( AA,*/< !switch to mode */< ,?T *R< MO( MO( '6D MO( MO( MO( MO( MO( MO( ";O.*7 ,?T ,?' ,?' D8' Z?L *R< D< &A D6 ";O.* !change a color )alue !next color of red !next color palette register !repeat for J1 colors !address offset RRRR !starting color number !display J1 colors !wait for any %ey !switch bac% to DOS )ideo mode AA,RARRR< 8S,AA '<,R '6,R D<,R &A,OR< AA,*R*R< D6,J1 !address segment ARRR with 8S !select increment !green )alue !blue )alue !red )alue !color register number OR< !change palette color function !count to change colors OR< to &9<

MO( D,,R MO( A6,OR< 'A66 &A?D MO( ,?T MO( ,?T .8A,T A<,* -*< AA,/ *R<

! !the &A?D procedure displays a color band of J1 colors !XXXinput parameterXXX !A6 D starting color number !8S D ARRR< !D, D starting offset address for display ! &A?D ";O' ?8A; MO( &<,1R !line count of 1R Page ;3 o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

&A?D*7 "0S< AA MO( 'A,J1 &A?D-7 MO( &6,2 &A?D/7 STOS& !store color D8' &6 Z?L &A?D/ ,?' A6 6OO" &A?D"O" AA D8' &< Z?L &A?D* ADD D,,/-RX*R ;8T &A?D 8?D" 8?D

!sa)e starting color number !color count of J1 !load times color is displayed

!repeat 2 times !get next color number !repeat for all J1 colors !restore original color number !repeat for 1R raster lines !s%ip *R raster lines

Title T"rogram *R+1# !a program that displays a green box on the )ideo screen using !)ideo mode */<. ! .MOD86 T,?@ .'OD8 .STA;T0" '6D !select auto+increment MO( ,?T MO( MO( MO( MO( 'A66 MO( ,?T MO( ,?T .8A,T AA,*/< *R< A6,'A,*RR S,,*R &",N2 &OA A<,* -*< AA,/ *R< !select mode */< !this also clears the screen !use color R-< (green !starting column number !starting row number !si>e !display box !wait for any %ey !switch to DOS )ideo mode

! !the &OA procedure displays a box on the mode */< display. !XXXinput parametersXXX !A6 D color number (R+-22 !'A D starting column number (R+/*P !S, D starting row number (R+*PP !&" D si>e of box ! &OA ";O' ?8A; MO( MO( "0S< MO( M06 &A,RARRR< 8S,&A AA AA,/-R S, !address segment ARRR with 8S !sa)e color !find starting "86

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ;+ o) *+

MO( ADD "O" "0S< MO( &OA*7 ;8" MO( S0& &OA-7 "O" ADD "0S< STOS& ADD S0& STOS& 6OO" "O" ADD MO( ;8" ;8T &OA 8?D" 8?D

D,,AA D,,'A AA D, 'A,&" STOS& 'A,&" 'A,D, D,,/-R D, D,,&" D,,-

!address start of &OA !sa)e starting offset address !sa)e si>e in &" !draw top line !ad:ust 'A !address next row !draw "86 !draw "86

&OAD, D,,/-R 'A,&" STOS& !address last row

Title T"rogram *R+2# !a program that displays a short cyan line that is *R "ixels wide !with a red dot below and to the right of the cyan line. ! .MOD86 T,?@ .'OD8 .STA;T0" MO( AA,RARRR< !address )ideo ;AM at segment ARRR MO( DS,AA '6D !select increment MO( ,?T MO( MO( MO( MO( MA,?*7 'A66 DOT ,?' S, 6OO" MA,?* MO( MO( MO( 'A66 MO( ,?T &A,1R S,,-RR D6,1 DOT A<,* -*< AA,*-< *R< 'A,*R &A,*R S,,*RR D6,/ !set mode to *-< !and clear screen !set dot count to *R !row address !column address !color / (cyan !plot *R dots !display one dot !repeat *R times !row address !column address !color 1 (red !display one red dot !wait for %ey

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ;? o) *+

MO( ,?T .8A,T

AA,/ *R<

!return to DOS )ideo mode

! !the DOT procedure displays one dot or "86 on the )ideo display. !&A D row address (R to 1NP !S, D column address (R to J/P !D6 D color (R to *2 ! DOT ";O' ?8A; "0S< "0S< MO( M06 MO( MO( MO( D,( MO( MO( ADD MO( S<; "0S< MO( MO( O0T MO( "O" O0T MO( MO( O0T MO( MO( O0T MO( MO( "O" "0S< O0T MO( "O" "O" ;8T DOT 8?D" 8?D 'A DA AA,OR &A D,,AA AA,S, D<,O D< '6,A< A<,R D,,AA A6,OR< A6,'6 AA DA,/'8< A6,O DA,A6 DA,/'9< AA DA,A6 DA,/'1< A6,DA,A6 DA,/'2< A6,R9< DA,A6 !sa)e color !find row address byte !sa)e it !find column address byte !get shift count !form address of "86 byte !find bit in bit mas% register !sa)e bit mas% !graphics address register !select bit mas% register !bit mas% register !get bit mas% !se3uence address register !select map mas% register !map mas% register !enable all planes

A6,BD,C !must read first &@T8 "T; BD,C,R !clear old color AA !get color from stac% AA DA,A6 &@T8 "T; BD,C,R99< !write memory DA 'A !restore registers

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ;= o) *+

Title T"rogram *R+J# !a program that display a cyan bar across the top of a white !screen. ! .MOD86 T,?@ .'OD8 .STA;T0" MO( AA,RARRR< !address )ideo ;AM at segment ARRR MO( DS,AA '6D !select increment MO( ,?T MO( MO( MO( MO( MA,?*7 'A66 &6O'= ,?' S, 6OO" MA,?* MO( MO( MO( MA,?-7 MO( MO( MA,?/7 'A66 ,?' 6OO" ,?' D8' Z?L MO( ,?T MO( ,?T .8A,T &6O'= S, MA,?/ &A D< MA,?A<,* -*< AA,/ *R< !display a bloc% !address next column !repeat OR times !increment row address !repeat 2- times !wait for %ey S,,R 'A,OR !column address !column count &A,* D6,N D<,2AA,*-< *R< 'A,OR &A,R S,,R D6,/ !set mode to *-< !and clear screen !bloc% count !row address !column address !color / (cyan !plot OR bloc%s !display a bloc% !address next column !repeat OR times !row address !color N (white !row count

!return to DOS )ideo mode

! !The &6O'= procedure displays one bloc% that is O pixels !wide by P pixels high. !&A D row address (R to 2!S, D column address (R to NP !D6 D bloc% color (R to *2 ! &6O'= ";O' ?8A; "0S< "0S< MO( MO( O0T MO( 'A DA DA,/'8< A6,O DA,A6 DA,/'9< !sa)e color !graphics address register !select bit mas% register !bit mas% register Page ;; o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

MO( O0T MO( MO( O0T MO( M06 MO( ADD MO( MO( "O" "0S< MO( &6O'=*7 MO( O0T MO( MO( MO( O0T MO( ADD 6OO" "O" "O" ;8T &6O'= 8?D" 8?D

A6,R99< DA,A6 DA,/'1< A6,DA,A6 AA,ORXP &A D,,AA D,,S, 'A,P DA,/'2< AA AA A<,A6

!enable all O bits !se3uence address register !select map mas% register !find row address byte !sa)e it !form address of "86 byte !byte count !map mas% register !get color

A6,R9< !enable all planes DA,A6 A6,BD,C !must read first &@T8 "T; BD,C,R !clear old color A6,A< DA,A6 &@T8 "T; BD,C,R99< !write memory D,,OR &6O'=* DA 'A

Title T"rogram *R+N# !program that display a bright red & at row R, column R, and a !cyan A at row 2, column -R. .MOD86 T,?@ .'OD8 .STA;T0" MO( AA,RARRR< !address )ideo ;AM at segment ARRR MO( DS,AA '6D !select increment MO( AA,*-< !set mode to *-< ,?T *R< !and clear screen MO( MO( MO( MO( 'A66 MO( MO( MO( MO( 'A66 A6,FAF D6,/ &A,2 S,,-R '<A; A6,F&F D6,*&A,R S,,R '<A; !display FAF !cyan !row 2 !column R !display cyan FAF !display F&F !bright red !row R !column R !display bright red F&F Page ;* o) *+

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

MO( A<,* ,?T -*< MO( AA,/ ,?T *R< .8A,T

!wait for %ey

!return to DOS )ideo mode

! !The '<A; procedure displays a character (O x O on the !mode *-< display without changing the bac%ground color. !A6 D AS',, code !D6 D color (R to *2 !&A D row (R to 2!S, D column (R to NP ! '<A; ";O' ?8A; "0S< "0S< "0S< "0S< MO( MO( ,?T "O" MO( S<6 S<6 S<6 ADD "O" M06 MO( ADD MO( '*7 MO( MO( MO( ,?' O0T MO( MO( O0T ,?' MO( MO( "O" "0S< O0T MO( ADD 6OO" "O" "O" ;8T '<A; 8?D" Page *< o) *+ DA,/'8< !address bit mas% register A6,O !load index O A<,8S7B&"C !get character row &" !point to next row DA,AA !modify bit mas% register DA,/'1< !address map mas% register AA,R9R-< DA,AA !select all planes DA A6,BD,C !read data &@T8 "T; BD,C,R !write blac% AA !get color AA DA,A6 !write color &@T8 "T; BD,C,R99< D,,OR !address next raster row '* !repeat O times DA 'A 'A DA &A !sa)e row address AA !sa)e AS',, AA,**/R< !get O x O set &<,/ *R< !segment is in 8S AA !get AS',, code A<,R AA,* !multiply by O AA,* AA,* &",AA !index character in ;OM &A !get row address MO( AA,ORXP !find row address &A D,,AA D,,S, !add in column address 'A,O !set count to O rows

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

8?D

Title T"rogram *R+O# !a program that displays two test lines of text on a cyan graphics !bac%ground screen. ! .MOD86 SMA66 .DATA M8S* D& FThis is test line *.F,R M8S- D& FThis is test line -.F,R .'OD8 .STA;T0" MO( AA,RARRR< MO( DS,AA '6D MO( ,?T MO( MO( MO( MA,?*7 MO( MO( MA,?-7 'A66 ,?' 6OO" ,?' D8' Z?L MO( MO( MO( MO( MO( MO( 'A66 MO( MO( MO( MO( 'A66 MO( ,?T MO( ,?T .8A,T &6O'= S, MA,?&A D< MA,?* AA,VDATA 8S,AA !display a cyan bloc% !address next column !repeat OR times !address next row !decrement row counter !repeat for 2/ rows !address data segment !with 8S 'A,OR S,,R !column counter !column R AA,*-< *R< D6,/ D<,2/ &A,R

!address )ideo ;AM !select increment !set mode to *-< !and clear screen !color cyan !row counter !row R

D6,P !bright blue text &A,2 !row 2 S,,R !column R D,,O99S8T M8S* !address M8S* 6,?8 !display bright blue M8S* D6,*!bright red &A,*2 !row *2 S,,R !column R D,,O99S8T M8S!address M8S6,?8 !display bright red M8SA<,* -*< AA,/ *R< !wait for %ey

!return to DOS )ideo mode

! !The line procedure displays the line of text addressed by 8S7D, !D6 D color of text (R to *2 . !The text must be stored as a null string Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page * o) *+

!&A D row !S, D column ! 6,?8 ";O' ?8A; MO( O; ZL "0S< "0S< "0S< 'A66 "O" "O" "O" ,?' ,?' ZM" 6,?8*7 ;8T 6,?8 8?D" ! !The '<A; procedure displays a character (O x O on the !mode *-< display without changing the bac%ground color. !A6 D AS',, code !D6 D color (R to *2 !&A D row (R to 2!S, D column (R to NP ! '<A; ";O' ?8A; "0S< "0S< "0S< "0S< MO( MO( ,?T "O" MO( S<6 S<6 S<6 ADD "O" MO( M06 MO( ADD MO( '*7 MO( MO( MO( ,?' O0T MO( MO( O0T DA,/'8< A6,O A<,8S7B&"C &" DA,AA DA,/'1< AA,R9R-< DA,AA !address bit mas% register !load index O !get character row !point to next row !address map mas% register !select all planes Page */ o) *+ 'A DA &A AA AA,**/R< &<,/ *R< AA A<,R AA,* AA,* AA,* &",AA &A AA,ORXP &A D,,AA D,,S, 'A,O A6,8S7BD,C A6,A6 6,?8* 8S D, S, '<A; S, D, 8S S, D, 6,?8 !get character !test for null !if null !sa)e registers !display characters !restore registers !address next column !address next character !repeat until null

!sa)e row address !sa)e AS',, !get O x O set !get AS',, code !multiply by O !index character in ;OM !get row address !find row address !add in column address !set count to O rows

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

,?' MO( MO( "O" "0S< O0T MO( ADD 6OO" "O" "O" ;8T '<A; 8?D"

DA A6,BD,C &@T8 "T; BD,C, R AA AA DA,A6 &@T8 "T; BD,C, R99< D,,OR '* DA 'A

!read data !write blac% !get color !write color !address next raster row !repeat O times

!The &6O'= procedure displays one bloc% that is O pixels wide by P pixels high. !&A D row address (R to 2!S, D column address (R to NP !D6 D bloc% color (R to *2 &6O'= ";O' ?8A; "0S< 'A "0S< DA MO( MO( O0T MO( MO( O0T MO( MO( O0T MO( M06 MO( ADD MO( MO( "O" "0S< MO( &6O'=*7 MO( O0T MO( MO( MO( O0T MO( ADD 6OO" "O" "O" ;8T &6O'= 8?D DA,/'8< A6,O DA,A6 DA,/'9< A6,R99< DA,A6 DA,/'1< A6,DA,A6 AA,ORXP &A D,,AA D,,S, 'A,P DA,/'2< AA AA A<,A6 A6,R9< DA,A6 A6,BD,C &@T8 "T; BD,C, R A6,A< DA,A6 &@T8 "T; BD,C,R99< D,,OR &6O'=* DA 'A 8?D"

!sa)e color !graphics address register !select bit mas% register !bit mas% register !enable all O bits !se3uence address register !select map mas% register !find row address byte !sa)e it !form address of "86 byte !byte count !map mas% register !get color

!enable all planes !must read first !clear old color !write memory

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page *2 o) *+

E-&eri"ent I Using t%e Mouse


Introduction: The mouse is an ,KO de)ice that replaces the arrow %eys on the %eyboard for graphical and text style programs. This experiment shows how to add the mouse to applications through a series of macros that enable and allow the mouse to function. Objectives: *+ De)elop macros that detect the mouse and enable it for applications. -+ De)elop macros that trac% the mouse position and test button status. /+ 0se the mouse in simple programs. I0T 22B: The mouse is controlled through ,?T //< function call instructions. There are actually more than 2R functions for mouse control. <owe)er, we will limit oursel)es to the most commonly used functions. These functions are listed in Table **.*. 1unction RR R* RR/ R1 Descri&tion ;eset Mouse Show Mouse 'ursor <ide Mouse 'ursor ;ead Mouse Status on the fly Set Mouse 'ursor "osition Entr# A< D RR< A< D R*< A< D R-< A< D R/< A< D R1< 'AD <ori>ontal 'ursor "osition DAD (ertical 'ursor "osition A< D R2< &AD Desired button R for left and * for right Table Test t%e "ouse: To be able to use the mouse it must be first tested whether it is present or not. To detect the presence of the mouse and be able to use it, the following steps are to be followed. Step *7 Test ,nterrupt (ector //< to see if it contains a )alue other than >ero. A >ero indicates that the mouse dri)er has not been installed yet. Step -7 ,f the )ector is not >ero, chec% if it points to an ,;8T ()alue '9< instruction. 9or some operating systems, an ,;8T indicates that the )ector is unused. Step /7 ,f the )ector is neither >ero nor does it point to an ,;8T instruction, then use the following code to test for the presence of the mouse. MO( AA, RRRR ,?T //< Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page *3 o) *+ E-it &A D ?umber of Mouse &uttons Displays the mouse cursor <ides the mouse cursor &A D &utton Status 'AD <ori>ontal 'ursor "osition DAD (ertical 'ursor "osition

R2

.et &utton "ress ,nformation

AA D &utton Status &A D ?umber of presses 'AD <ori>ontal "osition of 6ast "ress DAD (ertical "osition of 6ast "ress ! :Mouse (,?T //< 9unctions

,f a >ero is returned in AA, ther is no mouse otherwise, the mouse is present. The following MA';O tests for the presence of the mouse7 M" MA';O 6O'A6 M*, M-, M/ "0S< 8S MO( AA, /2//< ,?T -*< MO( AA, 8S O; AA, &A ZL M'M" &@T8 "T;B&AC, R'9< ZL MMO( AA, RRRR ,?T //< O; AA, AA ZL M'6' ZM" M/ D& */, *R, "0S< DS MO( AA, 'S MO( DS, AA !D,S"6A@ M* "O" DS ST' "O" 8S !,s mouse presentM !;ead )ector //< !Test for 8S7&AD RR !Test for R'9< !,f not, end macro !Start mouse !?o mouse !,f mouse, 'arry D R #XXX MO0S8 ";8S8?T XXX#

M* M-7

!Show no mouse !,f no mouse, carry D*

M/7 8?DM

Enabling t%e Mouse: The presence of a mouse does not mean that it can be used, unless it is enabled. The mouse cursor is enabled with ,?T //< function number R*<, and disabled with function number R-<. ?either of these functions returns any information to the caller. The following macros (see below turn the cursor O? and O99. The mouse cursor is off until the mouse dri)er is enabled. ,f the mouse cursor is enabled and data are displayed to the screen, the computer places a copy of the mouse pointer on the screen. ,f n items are displayed on the screen, the mouse pointer is also displayed n times. To a)oid this problem, the mouse pointer should always be turned O99 before updating the )ideo information, and then turned O? after the update is complete. MO? 8?DM MO99 MA';O MO( AA, RRR-< ,?T //< 8?DM !Disable Mouse "ointer MA';O MO( AA, RRR*< ,?T //< !8nable Mouse "ointer

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page *+ o) *+

Trac,ing t%e Mouse 5utton: Mouse ,?T //< function number 2 returns the button information and position of the last press. $hen called AA D 2 and &A D the button being tested D R, * and 1 for respecti)ely left, right and middle in case of a three button mouse. On return from function 2, AA gi)es the button status, i.e. if a button is being pressed. &it R &it * &it &A 'A DA D * for the left button D * for the right button D * for the middle button D number of times the button has been pressed, since the last time this function was called. D hori>ontal position D )ertical position

The following Macro is used for the abo)e purpose7 M&0T MA';O ?0M MO( AA, RRR2< MO( &A, ?0M ,?T //< !;ead &utton !?0M D R for left ! ?0M D * for right ! ?0M D 1 for middle

8?DM Trac,ing t%e Mouse Position: ,n the ORx-2+text mode, the )alues in 'A range from R to J/- and the )alues in DA range from R to *P- by increments of O. As an example line * position / returns 'A D O and DA D -1. 9unction 2 returns the mouse cursor position at the most recent button press, whereas function / returns the mouse position on the fly, i.e. in real+time, as it occurs. The following macro is used for that purpose. M;Time 8?DM T%e Mouse in :ra&%ics Mode: To ha)e a good understanding of how the mouse wor%s in )ideo mode, it is of benefit to try program **.-. To mode the mouse cursor to position A (hori>ontal and @ ()ertical , use ,?T *R< function R-<. 1unction R-< Descri&tion Mo)e Mouse 'ursor Entr# A< D RD< D 6ine ?umber D6 D 'olumn ?umber MA';O ?0M MO( AA, RRR/< ,?T //< !;ead Mouse Status

Table Pre $ab 4or,7

! /7 Mo)e 'ursor 9unction

*+ $rite all macros gi)en in the manual, and add them to your MA';OS.,?' file.
-+ $rite a program that tests the presence of the mouse using the macros gi)en in the text. /+ $rite a program that displays the word 689T if the left button is pressed and ;,.<T if the right button is pressed. 8xit the program if AA indicates that both left and right buttons are pressed together. Do not forget to turn off the mouse pointer before displaying 689T or ;,.<T, and turn it bac% on afterwards. 1+ &ring your wor% to the lab. Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page *? o) *+

$ab 4or,: *+ $rite, lin% and run program **+*. 'ompare the mouse pointer generated in graphics mode with the pointer generated in text mode. -+ Modify "rogram **.*, so that it displays the mouse position on the top right corner of the screen. 'all this program **./ Assign"ent: $rite a program that displays a green s3uare on the middle of the screen. 0se the mouse, so that when the mouse enters the s3uare, the color of the s3uare changes to red. Modify the abo)e program, so that when the mouse is inside the s3uare and you want to lea)e the s3uare red :ust press the left button. T,T68 G"rogram **+*G ,?'60D8 MA';OS.,?' .MOD86 SMA66 .STA'= -RR< .DATA .'OD8 .STA;T0" MO( AA, *-< ,?T *R< M" Z' MA,?MO? MA,?*7 M;T,M8 'M" &A, / Z?8 MA,?* MO99 MO( AA, R/< ,?T *R<

!Switch to mode *-< !Test for mouse !,f no mouse !8nable mouse pointer !;ead Mouse Status on+the+fly !Test for left and right buttons !,f both not pressed repeat !Disable mouse pointer !Switch to mode /

MA,?-7 .8A,T 8?D

T,T68 G"rogram **+-G !a program that displays the mouse pointer and its A and @ !position in text mode. ! .MOD86 SMA66 .DATA M8S D& */,FA "osition D F MA D& F F D& F@ "osition D F M@ D& F UF A D$ M !A position @ D$ M !@ position .'OD8 .STA;T0" 'A66 TMWO? !enable mouse Z' MA,?1 !if no mouse MA,?*7 MO( AA,/ !get mouse status Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page *= o) *+

,?T 'M" Z8 'M" Z?8 'M" Z8 MA,?-7 MO( MO( MO( MO( 'A66 MOV MO( 'A66 MO( ,?T MO( MO( ,?T MO( ,?T ZM" MA,?/7 MO( ,?T MA,?17

//< &A,* MA,?/ 'A,A MA,?DA,@ MA,?*

!if left button pressed !if A position changed !if @ position did not change

A,'A !sa)e new position @,DA D,,O99S8T MA AA,'A "6A'8 !store AS',, A DI,O99S8T M@ AA,@ "6A'8 !store AS',, @ AA,//< !hide mouse pointer A<,P DA,O99S8T M8S -*< !display position AA,* //< MA,?* AA,R //< !show mouse pointer !do again !reset mouse

.8A,T ! !procedure that tests for the presence of a mouse dri)er !XXXOutput parametersXXX !'arry D *, if no mouse present !'arry D R, if mouse is present ! '<=M ";O' ?8A; MO( AA,/2//< !get ,?T //< )ector ,?T -*< !returns )ector in 8S7&A MO( AA,8S O; AA,&A !test for RRRR7RRRR ST' ZL '<=M* !if no mouse dri)er 'M" &@T8 "T; 8S7B&AC,R'9< ST' Z8 '<=M* !if no mouse dri)er MO( AA,R ,?T //< !reset mouse 'M" AA,R ST' ZL '<=M* !if no mouse '6' '<=M*7 ;8T '<=M 8?D" !the TMWO? procedure tests for the presence of a mouse Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page *; o) *+

!and enables mouse pointer. !uses the '<=M (chec% for mouse procedure !XXXoutput parametersXXX !'arry D R, if mouse is present pointer enabled !'arry D *, if no mouse present TMWO? ";O' 'A66 Z' MO( ,?T '6' TMWO?*7 ;8T TMWO? 8?D" ?8A; '<=M TMWO?* AA,* //<

!test for mouse !show mouse pointer

!The "6A'8 procedure con)erts the contents of AA into a !decimal AS',, coded number stored at the memory location !addressed by DS7D, !XXXinput parametersXXX !AA D number to be con)erted to decimal AS',, code !DS7D, D address where number is stored ! "6A'8 ";O' ?8A; MO( MO( "6A'8*7 MO( D,( "0S< ,?' 'M" Z?8 "6A'8-7 MO( S0& "6A'8/7 "O" ADD MO( ,?' 6OO" 'M" Z8 MO( "6A'817 MO( ,?' 6OO" "6A'827 ;8T "6A'8 8?D" 8?D 'A,R &A,*R DA,R &A DA 'A AA,R "6A'8* &A,2 &A,'A DA D6,/R< BD,C,D6 D, "6A'8/ &A,R "6A'82 'A,&A !con)ert to AS',, !store digit !clear count !set di)isor !clear DA !di)ide by *R

!repeat until 3uotient R

&@T8 "T; BD,C,-R< D, "6A'81

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page ** o) *+

E-&eri"ent I / Serial Co""unications PC Inter)acing T%roug% T%e Serial Ports


Introduction: ,n general, most "'s ha)e two or more serial ports used to interface the "' to some peripherals, printer, modem, mouse, etc. ,n this experiment you will be introduced to the interfacing of two "'Fs through their serial ports. @ou will also transfer data between the two "'Fs. This experiment would be of interest to students who are ta%ing 'O8 /1- GData 'ommunicationG course. Objectives:

*+ Serial data transmission


-+ 0se of ,nterrupt *1< /+ "ort configuration. 1+ Sending and recei)ing data through the serial ports. Serial Port Progra""ing: The "' serial ports may be accessed using two different ways, either through direct programming, or using ,?T *1<. "rogramming directly the serial ports re3uires a deep understanding of the serial port hardware. Table *-.* gi)es the addresses of the different "' serial ports, and the associated interrupt numbers. Address IRE /9O < 1 -9O < / /8O < 1 -8O < / Table /! 7 Standard "ort Addresses <owe)er, in this lab we are going to restrict oursel)es to the use of ,?T *1< to access the serial port. I0T 3B: ,?T *1h supports the four sub+functions, shown in table *-.-. 1unction In&ut in Out&ut in E))ect RR A< ?o output ,nitiali>e Serial "ort R* A<, 'haracter in A6 Status in AA $rite a 'haracter to the Serial "ort RA< Status in AA ;ead a character from the Serial "ort R/ A< Status in AA .et status of the serial port Table /!/: ,nterrupt *1< The serial port number (R to / is specified in the DA register (RD'OM*7, *D'OM-7, etc. . ,?T *1h expects and returns other data in the A6 or AA register. The different uses of the functions of ,?T *1< are summari>ed in the following sections. AB R <: Serial Port Initiali'ation: Sub+function >ero initiali>es a serial port. This call lets you set the baud rate, select parity modes, select the number of stop bits, and the number of bits transmitted o)er the serial line. These parameters are all specified by the )alue in the A6 register using the following bit encoding7 0a"e 'OM * 'OM 'OM / 'OM 1

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page << o) *+

&its 2..N R..* /..1 * * * R R *

9unction &aud ;ate 'haracter Si>e Stop &its "arity &its 'haracter Si>e N bits O bits

J 2 &aud ;ate 1 R R **R R R * *2R R * R /RR * * * JRR * R R *-RR R * -1RR * R 1ORR R * * PJRR * Table /!2: 'onfiguring a serial port

N R R R R * * * *

/ R * R *

"arity ?o parity Odd parity ?o parity 8)en parity Stop bits One stop bit Two stop bits

MSDOS, "' &,OS, and 9ile ,KO7 Although the standard "' serial port hardware supports *P,-RR baud, some &,OSFs may not support this speed. E-a"&le7 ,nitiali>e 'OM*7 to -1RR baud, no parity, eight bit data, and two stop bits. MO6 AB9 < MO6 A$9 < << MO6 D79 < I0T 3B 5 MI0ITIA$IHE OPCODE MPARAMETER DATA! MCOM : PORT!

After the call to the initiali>ation code, the serial port status is returned in AA (see Serial "ort Status, A< D /, below . AB R : Trans"it a C%aracter to t%e Serial Port: This function transmits the character in the A6 register through the serial port specified in the DA register. On return, if A< contains >ero, then the character was transmitted properly. ,f bit N of A< contains one, upon return, then some sort of error occurred. The remaining se)en bits contain all the error statuses returned by the .etStatus call except time out error (which is returned in bit se)en . ,f an error is reported, you should use sub+function three to get the actual error )alues from the serial port hardware. E-a"&le7 Transmit a character through the 'OM* port MO6 D79 < MO6 A$9 PAQ MO6 AB9 I0T 3B TEST AB9 ;<B G0H SERIA$ERROR M SE$ECT COM : M CBARACTER TO TRA0SMIT M TRA0SMIT OPCODE M CBEC8 1OR ERROR

This function will wait until the serial port finishes transmitting the last character (if any and then it will store the character into the transmit register. ABR/: Receive a C%aracter )ro" t%e Serial Port: Sub+function two is used to read a character from the serial port. On entry, DA contains the serial port number. On exit, A6 contains the character read from the serial port and bit N of A< contains the error status. $hen this routine is called, it does not return to the caller until a character is recei)ed at the serial port. E-a"&le7 ;eading a character from the 'OM* port MO6 D79 << M SE$ECT COM : MO6 AB9 </ M RECEI6E OPCODE I0T 3B Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page < o) *+

TEST AB9 ;<B M CBEC8 1OR ERROR G0H SERIA$ERROR URECEI6ED CBARACTER IS 0O4 I0 A$V ABR2: Serial Port Status: This call returns status information about the serial port including whether or not an error has occurred, if a character has been recei)ed in the recei)e buffer, if the transmit buffer is empty, and other pieces of useful information. On entry into this routine, the DA register contains the serial port number. On exit, the AA register contains the following )alues7 A7 R * / 1 2 J N O P *R ** **/ *1 *2 5it Meaning 'lear to send Data set ready Trailing edge ring detector ;ecei)e line signal detect 'lear to send ('TS Data set ready (DS; ;ing indicator ;ecei)e line signal detect Data a)ailable O)errun error "arity error 9raming error &rea% detection error Transmitter holding register empty Transmitter shift register empty Time out error Table /!3: .etting the status of a serial port There are a couple of useful bits, not pertaining to errors, returned in this status information. ,f the data a)ailable bit is set (bit QO , then the serial port has recei)ed data and you should read it from the serial port. The Transmitter holding register empty bit (bit Q*/ tells you if the transmit operation will be delayed while waiting for the current character to be transmitted or if the next character will be immediately transmitted. &y testing these two bits, you can perform other operations while waiting for the transmit register to become a)ailable or for the recei)e register to contain a character. Pre $ab 4or,: *+ ;ead and understand material related to ,?T *1h. -+ $rite program *-.* and understand what the program is doing. /+ $rite program *-.- and understand what the program is doing. $ab 4or,:

*+ To be able to run the programs, you need to connect the "'Fs through their serial ports
-+ /+ 1+ 2+ J+ 'OM* or 'OM-, with serial cables terminated by DP type connectors. @ou will be supplied with such cables in the lab. @ou need also to wor% in groups of two. ;un program *-.* and notice what the program is doing. ;un program *-.- and notice what the program is doing Modify program *-.* to ma%e it send at a baud rate of PJRR. Modify program *-.* to ma%e it send a string of / characters. Modify program *-.- to ma%e it recei)e a string of / characters at a baud rate of PJRR.

$ab Assign"ent: De)elop a program that displays a menu consisting of the following choices7

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page </ o) *+

Con)igure Serial Port: prompts the user to enter one of the standard baud rate )alues. The program then configures the 'OM* port with the gi)en baud rate. (The rest of the configuration with default )alues Send String: prompts the user to enter the length of the string and then enter the string character by character. The program sends each character as soon as it is entered. Receive String: prompts the user to enter the length of the string. ,t then recei)es the string character by character and displays it on the screen. Send 1ile JO&tionalK: prompts the user to enter the location of the file and then sends that file. (?eeds more information about handling files. As% the instructor for ;eferences to that information. Receive 1ile JO&tionalK: recei)es the file. (?eeds more information about handling files. As% the instructor for ;eferences to that information.

T,T68 T"rogram *-.*# !This program configures the serial port ('OM* and sends a character through that port. .MOD86 SMA66 .STA'= -RR .DATA ";OM"T S8?T 8;;O; .'OD8 .STA;T0" MO( A<, R< MO( A6, *R*RR***& MO( DA, R< ,?T *1< 68A DA, ";OM"T MO( A<, RP< ,?T -*< MO( A<, R*< ,?T -*< MO( DA, R< MO( A<, *< ,?T *1< T8ST A<, OR< Z?L "O;T8;;O; 68A DA, S8?T MO( A<, RP< ,?T -*< ZM" 9,?,S< "O;T8;;O;7 68A DA, 8;;O; MO( A<, RP< ,?T -*< 9,?,S<7 .8?D ! ,?,T,A6,L8 O"'OD8 ! "A;AM8T8; DATA. ! 'OM*7 "O;T. ! D,S"6A@ ";OM"T O? S';88? D& D& D& T8nter the character to be sent#,RD<,RA<,#U# TThe character has successfully been sent#,#U# TAn error occurred. "lease chec% your hardware and try again.#,#U#

! ;8AD T<8 '<A;A'T8; 9;OM =8@&OA;D ! S868'T 'OM*7 ! T;A?SM,T O"'OD8 ! '<8'= 9O; 8;;O; ! D,S"6A@ T<AT T<8 '<A;A'T8; $AS S8?T

! D,S"6A@ T<AT T<8;8 $AS A? 8;;O;

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page <2 o) *+

T,T68 T"rogram *-.-# !This program configures the serial port ('OM* and recei)es a character through that port. .MOD86 SMA66 .STA'= -RR .DATA ";OM"T D& T;ecei)ing character. "lease wait.#,RD<,RA<,#U# ;8'8,(8D D& TThe character has been recei)ed.#,RD<,RA<,#U# 8;;O; D& TAn error occurred. "lease chec% your hardware and try again.#,#U# '<A; .'OD8 .STA;T0" MO( A<, R< MO( A6, *R*RR***& MO( DA, R< ,?T *1< 68A DA, ";OM"T MO( A<, RP< ,?T -*< MO( DA, R< MO( A<, R-< ,?T *1< T8ST A<, OR< Z?L "O;T8;;O; 68A DA, ;8'8,(8D MO( A<, RP< ,?T -*< 68A DA, '<A; MO( A<, RP< ,?T -*< MO( D6, A6 MO( A<, R-< ,?T -*< ZM" 9,?,S< "O;T8;;O;7 68A DA, 8;;O; MO( A<, RP< ,?T -*< 9,?,S<7 .8?D ! D,S"6A@ '<A; M8SSA.8 ! ,?,T,A6,L8 O"'OD8 ! "A;AM8T8; DATA. ! 'OM*7 "O;T. ! D,S"6A@ ";OM"T O? S';88? D& TThe recei)ed character is7 T,#U#

! S868'T 'OM*7 ! ;8'8,(8 O"'OD8 ! '<8'= 9O; 8;;O; !D,S"6A@ T<AT T<8 '<A;A'T8; $AS ! ;8'8,(8D

! D,S"6A@ T<8 '<A;A'T8; O? S';88?

! D,S"6A@ T<AT T<8;8 $AS A? 8;;O;

Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing

Page <3 o) *+

!XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ! ";O'. D,S"6A@ ;8S06T ;8S06T ";O' ?8A; MO( 'A, RRRO ! D,S"6A@,?. '6' ?8AT7 ;'6 &6, * Z?' &,TWR MO( D6, F*F MO( A<, R-< ,?T -*< ZM" 6AST &,TWR7 MO( D6, FRF MO( A<, R-< ,?T -*< 6AST7 6OO" ?8AT ;8T ;8S06T 8?D" 8?D ! TIT$E @PRO:RAM / E7PERIME0T ?A ! This program shows how to manipulate a two+digit numbers .MOD86 SMA66 .STA'= -RR .DATA ?0M* D& M ?0M- D& M .'OD8 .STA;T0" ! ;8AD ?0M&8; ?0M* MO( A6, ?0M* A?D A6, R9< MO( '6, R1< S<6 A6,'6 MO( &6, A6 MO( A6, ?0M* A?D A6, R9R< MO( '6, R1< S<; A6, '6 O; &6, A6 MO( ?0M-, &6 ! D,S"6A@ ?0M&8; ?0M* ! 'O?(8;T ?0M- TO &,?A;@ ! D,S"6A@ ?0M&8; ?0M.8A,T 8?D Co"&uter Organi'ation w( Asse"bl# $anguage Progra""ing Page <+ o) *+

You might also like