ELEC 242 Using Library Procedures There are a number of existing procedures that are already written for you that you will use in your programs. In order to use the library procedures that come with the text, you must install the example programs that come on the CD-ROM, or download the library from the authors web site at http://www.nuvisionmiami.com/books/asm/files/irvine.zip. You will need to unzip the file to your work disk and directory. The name of the file is irvine.lib. You can use the Windows find file to locate exactly where Irvine.lib is located on your hard drive. In order to use the procedures in this library, the library file must be on your data disk in the same directory as your asm and obj files. You should copy the file from your hard drive to your data directory (the root directory on the 3 ½ inch Floppy.) The files indicated by the EXTRN directive are accessed by the linker. To use a procedure in the library, we must identify the procedure name in the area of our source code file. The EXTRN directive should be laced in the code segment of our source code file immediately after the directive. In order to link to an external library file, you must name the library in the command line. The figure below shows that the Link Library is input to LINK.EXE just as the obj file. Therefore, when we invoke ML, we must identify the library. In addition, The modified ml command line is: ml /Fl irvine.lib filename.asm To make this easier, you may wish to create a batch file called go.bat. It would have one command in the file: ml /Fl irvine.lib %1.asm To assemble and link a file called lab07.asm using the batch file go.bat, go to the system prompt and enter the command: go lab07 where lab07 is the name of the asm file that you created. The command is entered without the extension. We can use this batch file even if we are not using a procedure in the library. We could identify a library in the command line even if there are no EXTRN directives in our asm file. In this case, nothing needs to be accessed form the library, so no error or warning will be generated. However, if we have an EXTRN directive and no library is accessed from the command line, error messages may be generated. This is accomplished by placing the name of the external procedures that we wish to use in the code segment, preferably immediately after the or directive. The procedures that we wish to use are identified as ProcName:proc where ProcName is the external procedure name. If more than one external procedure is required, each is comma delimited. If many procs are being used, we may continue on the next line by placing a, \ at the of the current line. (If you forget the comma before the \ you will receive an error message from the assembler). The line with the EXTRN directive would be similar to the following: Extrn Proc1Name: proc, Proc2Name: proc, \ Proc3Name: proc, Proc3Name: proc Page 1
ELEC 242 Using Library Procedures - writestring WriteString is a procedure that allows you to easily s a string of data stored in a null terminated array to the display. We performed a similar task already using Function 9 INT 21H with our Hello World program. Function 9 requires a string array terminated by $. To use the WriteString library procedure, we must create an null terminated (the last character of the array is,0) array of characters that provide the message. The program requirements are: A null terminated string must exist in the data segment. The base address of the null terminated string data must be placed in DX. The string (without the 0) is present on Standard Output (the display) To use WriteString, we might enter the following in the segment: cr equ 0dh lf equ 0ah greet db cr,lf, Welcome to Brookdale Community College. db cr, lf, 0 WriteString is an external procedure that exists in irvine.lib. We must identify the name of the procedure in irvine.lib that we wish to use. TITLE Program Hello World by Andrew H. Andersen, Jr. PAGE 60,80 ; Brookdale Community College.model small.stack 2000H cr equ 0dh lf equ 0ah greet db cr,lf, "Hello world",cr,lf,lf,0 done equ 04c00H extrn WriteString: proc ; This code uses a procedure in irvine.lib to display a message lea dx,greet ;or mov dx,offset greet ;an external procedure fini: mov ax,done int 21h Page 2
ELEC 242 Using Library Procedures - WriteInt WriteInt WriteInt is another procedure that has been written for you and is part of irvine.lib. WriteInt is a procedure that allows you to easily write unsigned, 16-bit integer data to the standard output in ASCII. This means that the range of values are 0 through 65 535. The data may be written binary, octal, decimal, or hexadecimal format. This procedure will perform the conversion to the desired number base, convert each digit to ASCII, and s the character to the console output. The 16-bit unsigned integer data must be in AX. Numeric value range is 0 to 65 535 The radix for the format of the desired numeric display format must be in BX. The radix is entered as 2,8,10,16. Any other data in BX is invalid. The numeric data is present on the display in the specified number base. To use a procedure in the library, we must identify the procedure name in the area of our source code file. The EXTRN directive should be laced in the code segment of our source code file immediately after the directive. The beginning of the code segment is similar to the following: extrn Writeint:proc ; The general setup for WriteInt with 16-bit data is: mov ax,num ;put data value in AX mov bx,10 ;put desired numeric format in bx call WriteInt ;call the procedure WriteInt Notice that this requires a single piece data to be in a word-sized data storage buffer called data. For byte-sized data, the program would have to be modified as follows: xor ax,ax ;Clear the AX register (we need AH to be 0) mov al,data ;put byte value in AL ; since ah is 0, we have a proper 16-bit value mov bx,10 ;put desired numeric format in bx call WriteInt ;call the procedure WriteInt The proceeding is fine for single value of data. Suppose we wish to write an array of 16-bit unsigned integers. We would need to use word pointer to the array, and a loop counter that is incremented by 2, and a word pointer. The following is an example of a procedure with its required data segment to use WriteInt to display word-sized unsigned integers from an array called values with nvals as the number of entries in array. array dw 24 dup (1) ;init to all 1's nvals equ (($-array)/2) ;($-array)/2 since there are 2 bytes/value ; The previous statement automatically calculates ; the number of elements in the array Page 3
ELEC 242 Using Library Procedures - WriteInt extrn Writeint: proc, Crlf: proc ;call to library procedure ; some stuff may be here ; Assume it fills an array with word-size integer data. ; We will use nested procedures to Call ShowIt to walk the array ; and call a library procedure Writeint ; to s the value to the display ; and another procedure Blank to move the cursor to a new line lea si,array ;load SI as an address pointer to values mov cx,nvals ;initialize the loop counter mov bx,10 ;load the number base to display again: mov ax,word ptr [si] ; Get value from the array using word ptr call Writeint ; use a library function to show the value call crlf ;only if you wish to place each ; value on a separate line add si,2 ;Increment address pointer by 2 loop again ;Continue to again until CX is 0 mov ax,4c00h ;normal MS-DOS exit int 21H To write multiple numbers on the same line, modify the code to the following: array dw 24 dup (1) ; init to all 1's nvals equ (($-array)/2) ;($-array)/2 since there are 2 bytes/value blank db,0 ; or replace the single space in the quotes ; with 20h,0 extrn Writeint: proc, Crlf: proc, WriteString: proc ; some stuff may be here lea dx, blank lea si,array ;load SI as an address pointer to values mov cx,nvals ;initialize the loop counter mov bx,10 ;load the number base to display Page 4
ELEC 242 Using Library Procedures - WriteInt again: mov ax,word ptr [si] ; Get value from the array using word ptr call Writeint ; use a library function to show the value ; to print 1 space between values add si,2 ; Increment address pointer by 2 loop again ; Continue to again until CX is 0 mov ax,4c00h ; normal MS-DOS exit int 21H Page 5
ELEC 242 Using Library Procedures - ReadInt ReadInt ReadInt is the fourth procedure that we will discuss and is part of irvine.lib. ReadInt is a procedure that allows the user enter a signed, 16-bit binary integer in ASCII from Standard Input (the keyboard). Since this requires user input, you should print a prompt (a message that tells the user what to do) to Standard Output You should print an appropriate prompt on the display If AX contains data that you will need later, save it in Storage or on the Stack The 16-bit signed binary integer data is returned in AX. indata dw 24 dup (1) ;init to all 1's nvals equ (($-indata)/2) ;we used ($-array)/2 since there are 2 bytes/value prompt db Enter a number ==>,0 greet db You will have to enter 24 numbers., 0 The beginning of the code segment is similar to the following: extrn Readint:proc, WriteString:proc, crlf:proc ;The general setup for ReadInt is: lea dx,greet ;or mov dx,offset greet call Crlf lea dx,prompt ;or mov dx,offset greet push ax ;only if it contains useful data mov si,offset indata ;point to address of word size data mov cx, nvals ;load loop counter with number of words more: ;an external procedure to display prompt call ReadInt ;get the number call Crlf ;go to next line mov word ptr [si],ax ;put in array add si,2 ;increment 1 word loop more ;until cx = 0 pop ax ;get ax back ;do whatever has to be done here mov ax,4c00h ;normal MS-DOS exit int 21H Page 6
ELEC 242 Using Library Procedures - ReadString ReadString ReadString reads a string of characters from Standard Input and stores it as a null terminated string. This is the reverse of WriteString. We need to perform a number of steps to use this procedure. 1. We need to setup storage in memory (RAM) for the input data. Since the string is null terminated, make the array 1 byte larger than the number of characters you wish to store. 2. We need to define the size of the storage area. 3. Since the user must do something, provide a prompt to tell them what to do. You should print an appropriate prompt on the display telling the user what to do. You must have storage setup that is one byte larger than the number of characters Initialize DX to the base address of the string. Initialize CX to the maximum number of characters to accept AX contains the number of characters entered from Standard Input. The String is written to RAM. Your_Name db 26 dup (?) ;init to all 1's Num_Char equ $-indata-1 ;we use $-array-1 since we need an extra byte for the null prompt db Enter your name ==>,0 result db Your name has,0 nextmsg db characters.,0 The code segment is similar to the following: extrn WriteString: proc, ReadString: proc, \ WriteInt: proc, ReadInt: proc mov mov ax,@data ds,ax lea dx, prompt lea dx, Your_Name mov cx, Num_Char call ReadString lea dx, result mov bx,10 call Writeint lea dx, Nextmsg mov ah,4ch int 21h Page 7