Quick viewing(Text Mode)

Turbo Pascal & Assembly

Turbo Pascal & Assembly

.is-t '~,f; ~ . ~ ~

$1 03 $278 $389 $291 $1 16 $389 S350 S343 $103 S304

S399 $389 S349 By Michael Bertrand

urbo Pascal is a integer; in AH always signifies the DOS service S454 supple lan­ end; being called. In Turbo Pascal, a dollar $259 guage. Still, sign ($) prefix before a numerical con­ $376 $382 Tsome problems var registers: reg; stant means the number is in hexadeci- S499 require an solution. mal notation (base 16). , $974 $579 These problems fall into two broad cate­ The ax, bx, etc., are record fields of For example, we obtain the system S252 gories: speed (for example, disk I/ O) and type integer that are given the same time as follows in assembler: $369 direct access to the system (for example, names as some of the 8088 registers. As­ $350 $350 get the ). signments are made to these Turbo Pas­ mav ah,2Ch ;get time service S324 Often we want to combine the flexibil­ cal fields, and then MSDOS is executed. int 21 h ;DOS call S499 ity of Turbo Pascal with the power of as­ Values returned by the DOS call in reg­ $779 sembly language, which can be done by isters are recovered by Turbo Pascal in The time is returned in the following executing short assembly language rou­ variables of the same name: registers: tines within a Turbo Pascal program. In­ formation can also be passed between the registers.ax,= $0200; {AH=2, AL=0} CH=hour (0-23) S1 09 Pascal and assembly language programs. msdos(registers) {INT 21 h} CL=minute (0-59) $1 94 S 83 Unlike Microsoft languages, 's DH=secand (0-59) Turbo Pascal does not create OBJ files Recall that the 8088's AX register is DL = hundredths/sec (0-99) S 83 that can be linked with modules created 16 bits wide-the same as Turbo Pascal by other or assemblers. But integers. AX can be broken into a high There is no built-in way to obtain the Turbo Pascal does provide other ways to byte (AH) and a low byte (AL). Simi­ system time in Turbo Pascal, but we can S 42 integrate assembly language code into a larly, BX = (BH, BL), CX = (CH, get the time by executing the DOS call S 51 Pascal program. Some of these methods CL), and DX = (DH, DL). The value in Listing 1. $174 S 25 are discussed in this article. S 77 S122 MSDOS and INTR procedures Turbo Pascal's MSDOS procedure is procedure get time ( var hour, minute, se=nd, hundn..uth: byte J ; used to execute interrupt 21h, a DOS type regs ·= rec'Ord call. Interrupt 21h provides a collection ax,bx,cx,dx,bp,cii,si,ds,es,flags: integer of services that can be used to dis­ end; play characters, read the keyboard, open var registers.: regs; disk files, and access the machine in oth­ er ways. Interrupt 21 h is the domain of lx..,gin assembly language , but with registers do Turbo Pascal's MSDOS procedure makes begin interrupt 21h available to Turbo Pascal ax:= $2C00; [AH= 2Ch; AL= 00h} 0 programmers. rnsdos(registers); [INT 21h} In implementing the MSDOS proce­ hour := hi(cx); {CH} dure, a Turbo Pascal record type and minute:= lo(cx); (CL} variable are used: sec--ond := hi(dx); [DH) hundredth:= lo(dx) (DL) type reg = record end [with} ax,bx,cx,dx, bp ,si,di,ds,es, Flags: end; [procedure} Listing 1. 51 The Turbo Pascal INTR procedure is For example, we can print the screen change the cursor appearance. This is used to execute any interrupt-MSDOS from within an assembly language pro­ done in assembler as follows: is a special case for interrupt 21h. The gram by executing an interrupt 5h (this REGISTERS record is used with INTR, interrupt is invoked when mov ah,01 h ;set cursor service s just as with MSDOS, except that the in­ is pressed at the keyboard): mav ch,hi_scan_line ;scan lines: 0-7 terrupt number must be given: ;if color/graphics C int 5h ;print the screen mav cl,la_scan_line ;scan lines: 0-12 t registers.ax : =$0100; AH= 1,AL=O} ;if monochrome registers.ex : =$0607; !CH=6,CL=7} The same thing is done within a Turbo int I Oh ;video services intd$10,registers); INT lOh} Pascal program by: Different shaped cursors result from c Any of the BIOS services can be intr($05 ,registers); assigning different values to CH and CL. t called: video services (INT 10h), disk Some possible assignments for the eight services (INT 13h), keyboard services In the next example, we invoke one of scan lines on a color monitor are shown (INT 16h), and so on. the video services at interrupt 10h to in Figure 1. If we wish to turn the cursor offalto­ l gether, we assign a greater value to CH r than to CL: G s Value assignments for CH and CL TURN t CURSOR hi_scan_line (CH) = 5 I e Full OFF: la_scan_line (CU = 3 block ...... 0 hLscan_line (CH) = 0 C cursor: ...... I With INTR, we can also execute this 0 ········ 2 video BIOS call in Turbo Pascal, as t ...... 3 shown in Listing 2. l ...... 4 \I ...... 5 INLINE statement 0 ...... 6 Turbo Pascal's /NL/NE statement pro­ 7 lo_scan_..Jfrie (CU = 7 vides a way to insert di­ f ...... rectly into a Turbo Pascal program:

inline($B4/$02/ {mov ah,2} .Lower two $CD/$21 ); {int 21 h} scan 0 lines 1 $B4/$02 are the two bytes of machine g 2 language representing the 8088 instruc­ s 3 tion mov ah,2, and $CD/$21 is the ma­ l 4 chine language for int 21 h. The four l 5 bytes are actually inserted into the com­ 6 hLscan_line (CH) = 6 piled Pascal program at this point. Turbo fi ...... 7 IR...scan-line (CU - 7 Pascal syntax requires that the bytes be n separated by a slash mark (/) and that a the sequence of bytes be parenthesized. 1 ' We programmers think in terms of b Middle mov ah,2, but /NL/NE requires us to de­ l lour scan 0 lines I termine the corresponding machine lan­ p ...... 2 hi_scan-line (CH) = 2 guage representation. This can be done II 3 with the simple assembler in DEBUG. C 4 For more complicated assembly language 5 lo-scarL.line(CU = 5 routines, especially those involving labels, 54 6 LST files can be used. These files, gener­ SI 7 ated by the macro assembler, contain the b assembly language side-by­ side with the corresponding machine language. Figure l.

52 COMPUTER LANGUAGE ■ SEPTEMBER 1987 A disk file can be read into memory variable fi/e_name with readln. As al­ OK will then register as FALSE when much more quickly in assembler than ready discussed, the IN LINE code finds we return to Turbo Pascal. with Turbo Pascal's READ. Listing 3 the address of this variable with: Somewhat different methods are need­ e shows an example of INLINE code to ed to find variables other than global open a disk file, read a specified number $8D/$16/file_name/ {lea dx,file_name} variables. The instructions in the IN­ of bytes into a Turbo Pascal array, and LINE example depend on the fact that then close the file. However, Turbo Pascal uses the first byte Turbo Pascal maintains its global varia­ The file is opened with the "open a of a string variable to store the number bles in the data segment. Different kinds file" service at interrupt 21h (AH = of characters currently assigned to the of variables are stored differently: 3D). This service requires that the ad­ string variable. The string itself-the file dress of the file name first be loaded into name in this case-is stored starting at Where T the DX register: the second byte. This is the reason for Kind of variable ~t the next instruction: variable stored n lea dx,file_ name Global Data segment inc dx Local Stack segment )­ Upon return from interrupt 21 h, the AX Variable parameter Address in H. register contains the file handle, a 16-bit Turbo Pascal takes an entire byte to stack segment quantity that is identified with this file in store a Boolean variable, with the infor­ Typed constant Code segment subsequent operations. This handle must mation residing in the least significant be specified to read or close the file, for bit: l = TRUE, 0 = FALSE. The disk­ Turbo Pascal externals 5 example. related DOS calls signal an error by set­ Externals are COM files inserted direct­ 3 Turbo Pascal global variables are de­ ting the carry flag. If we find the carry ly into a Turbo Pascal program. Param­ clared to hold the file name, handle, and flag set in the !NL/NE routine, we put a eters are passed on the stack. Turbo is other relevant information (BUFFER, 0 in Boolean variable OK: Pascal views the COM file as a proce­ the array into which the file will be read; dure. The external declaration must be FILE--..SIZE; and OK, which tells mov ok,O followed by the name of the COM file, whether the file was successfully opened). t- The !NL/NE code finds the Turbo Pascal variables by direct reference: procedure set cursor (hi scan line, lo,,, scan line: byte); type regs = record - - - - $8D/$16/file_ name/ {lea dx,file_name} ax, bx, cx,dx, bp,di, si, ds, es, flags: intt..'Cjer end; An LST file would contain the two-byte var registers: regs; $8D/$16 opposite the lea instruction, to­ ch,cl: byte; me gether with a two-byte address. As c­ shown previously, we simply fill in a begin t- Turbo Pascal global variable name with registers do (fi.le_name) . begin m­ In effect, the Pascal program sends in­ ax:= $01.00; (AH= 01.h; AL= 00h} rbo formation to the IN LINE routines (file ch:= hi scan line; :>e name, file handle, and buffer address) cl:= l™ scan line; lt and receives information from the IN­ ex:= 256*ch +-cl; (GI is high byte of ex} l. LINE routines (file handle, number of (CL is lo,, byte of ex} bytes actually read, and error status). intr($10,registers) {INr 10h} de­ This two-way transfer of information is end · (with} end; (procedure} n- possible because the IN LINE code can 1e locate the addresses at which Turbo Pas­ cal variables are stored. Listing 2. age To make this transfer work, you need els, some knowledge of how Turbo Pascal 1er­ stores variables. The name of the file to the be opened is read into the Pascal string (-

53 -

program read file into buffer; const r.ax size = 1000;- var file name: string[50]; buffer: arrayll. .,nax_size] of byte; file size, handle: integer; ok: - boolean; (*------*) procedure get name open file; lx.->gin - - - clrscr; write('ENTER PATH AND NAME OF FILE 'ID READ: '); readln(file narre); file name:;; file name+ chr(0); (ASCIIZ form:,t} ok :;; true; ( TRUE = 01h in merrory}

inline ($B0/$00/ (ITDv al,0 ;read only access $B4/$3D/ (ITDV , ah,3Dh ; open a file service $80/$16/file name/ (lea dx,file name ;DX<-- addr of Turbo var $42/ - (inc dx - ;go to string data $CD/$21/ (int 21h ;OOS call ( :NarE: AX<-- handle $A3/handle/ (ITDV handle,ax $73/$05/ (jnc instr after next $C6/$06/ok/$00) (ITDV ok,0 ;ok = FAI..SE(00h) end; (procedure) (*------*) procedure read file; o...<>gin - inline ($8B/$1E/handle/ (ITDV bx,handle ;BX<-- handle $B4/$3F/ (ITDv ah,3Fh ;read a file service $8D/$16/buffer/ (lea dx,buffer ;DX<-- addr of Turbo var j' $8B/$0E/file size/ (ITDv cx,file size ;# bytes to read ) $CD/$21/ - (int 21h - ;00S call } ( ;AX= #bytes actually read} $A3/file size/ (ITDv file size,ax } $B4/$3E; (rrov ah,JE ;close file service } $CD/$21) (int 21h ;00S call } end; [procedure} (*------*) begin (main} get name open file; (name in file_name, error status in ok) if ok then - begin file size:= max size; read-file (NOTE: file size has been adjusted} end else write( 'ERROR') end.

Listing 3 .

54 COMPUTER LANGUAGE • SEPTEMBER 1987 I .... -- - '' - L.

between quotes, as it appears in the disk stack. Here, two integers, or four bytes, directory. are passed, hence rel 4. Listing 4 is a Turbo Pascal program to pop bp Procedure FILL(ATTR, CH) has two fill the color/graphics screen with a pre­ rel 4 parameters in the Pascal program­ scribed attribute and character. Both of ATTR is the first parameter and CH is these parameters are passed to a COM The operand of rel will vary depending the second. The parameters are found in external that does the filling (fast). on how many bytes are passed on the the COM file relative to the base pointer FILL.COM must be on the default drive for program fi!L.screen to compile. FILL.ASM, the source code for FILL .COM, is shown in Listing 5. Recall that program fill screen; FILL.ASM must be assembled with the var attr, ch: integer; macro assembler, linked, and converted procedure fill(attr,ch: integer); external 'fill.can'; to a COM file with EXE2BIN to pro­ duce FILL.COM. begin I); The first two and last two instructions write( 'ENTER ATTRIBUTE, 'I'HEN QIARACTER: read(attr, ch); of the COM file must be: fill(attr, ch) [NCYI'E: attr is FIRST parameter) end. [ ch is SECOND pararreter) push bp mov bp,sp Listing 4.

;FILL.ASM -- For use as an assembler external with Turbo Pascal.

code segment ;this must be =nverted to FILL.COM asswne cs:code ; TAKES 2 INTffiER PARAMEI'ERS

fill pr oc near push bp ;these two statements are necessary rrov bp,sp ;the passed parameters are on the stack ;and BP is used to point to them ;BP, CS, DS, and SS must always be preserved ------push ds ;old DS must be saved -- it's changed here rrov ax, 08800h ;we are going to poke directly into screen RAM ;assumes =lor card -- would be 0B000h on r.ono llDV ds,ax ;DS <-- 0&300h HOV al,[bpJ+4 ; the 2nd parameter is at [BP]+4 (the character) rrov i:ih, [bp]+6 ;the 1st pararneter is at [BP]+6 (the a ttribute) nov cx,1920 ;nLlfllber of character/attributes to write nDV bx,0 ;BX will point to screen offset again: l:DV [bx],ax ;loop to fill the top 24 lines inc bx inc bx ;increment twice because the rrov writes tvJO bytes loop again p:Jp ds ; restore Turbo Pascal's data segment

pop bp ;t11ese two statements are necessary ret 4 ;The 4 is required because four bytes were passed on ;t11e stack as parameters. The 'ret' increments ;the stack pointer (SP) by 2, but we need to inc- ; rement SP by another 4 to get ruck to 'l'urbo' s SP. fill endp · o:xle enus end Listing 5. 55 register (BP), which is used to locate in­ ternal COM file. The COM file then INT 3 is coded as a single byte ($CC) , formation in ihe stack segment. knows where the Turbo Pascal variable is so this !NL/NE statement will generate BP locates the parameters in the order and can access it freel y. As with IN­ a in a Turbo Pascal program: opposite to which they were passed. The LINE code, information can be sent in second, or last, parameter (CH) is found both directions through Turbo Pascal inline($CC) at [BP] +4; the first, or next-to-last, pa­ variables. rameter (ATTR) is at [BP] +6. This technique is invaluable for debug­ The last parameter is always at DEBUG ging. For example, in the COM file ex­ [BP] + 4, but the location of the next-to­ Interrupt 3 generates a DEBUG break­ ternal, we can insert a breakpoint just last parameter depends on the size of the point. This means that when a program before the call to the external: las t parameter. If the last parameter running under DEBUG control encoun­ were a string/3), which is four bytes ters an INT 3, the program stops execu­ read(attr, ch); long, for example, then the next-to-last tion, the 8088 registers and flags are inline($ccl; parameter would be at [BP]+ 8. The sec­ displayed, and control reverts to the DE­ fill(attr, ch ) ond-to-last parameter would be at some BUG command line. From here you can sti ll higher address, and so on. The stack single step through a compiled program If we then run the Turbo Pascal program of FILL.COM is shown in Figure 2. and examine memory, whether Turbo as a COM file under DEBUG control, Turbo Pascal allows a program to Pascal variables or stack. the Pascal read will function as usual know the address of any of its variables. This process is easiest if you first com­ and wait for our input. But after pressing If VAR_NAME is a Turbo Pascal vari­ pile the Turbo Pascal program as a Enter, control reverts to DEBUG and we able, then OFS(VA R_NAME) is the var­ COM file and then execute the COM can then begin single stepping through iable's offset address and SEG file under DEBUG control. To single step the program. After a few instructions in­ (VA R_NAME) is the va riable's segment through a breakpoint, reset the instruc­ serted by the , we come to the address. tion pointer (IP) to the byte after the external: These addresses are integers and can INT 3, then resume single stepping (oth­ be passed as integer parameters to an ex- erwise you never get past the INT 3) . push bp mov bp,sp push ds

I STACK in FILL.COM and so on. The stack can be viewed to re­ solve any question about the location of parameters. n

Low addresses References Bradley, David J. Assembly Language Pro­ gramming /or the IBM Personal Computer. Turbo's DS: DS Prentice-Hall, 1984. [A thorough introduc­ tion to 8088 assembly language. Includes a Turbo's BP: BP - FIL L's BP points here number of topics on the IBM PC.] Lafore, Robert. Assembly Language Primer Return address, IP - BP + 2 points here for th e IBM PC & XT. Plume/ Waite, 1984. [A fi ne book for beginners. Doesn't 2nd parameter: CH - BP + 4 points here assume you already know the subject.] Norton, Peter. 's Guide to the 1st parameter: ATTR - BP + 6 points here IBM PC. Microsoft, 1985. [A good refer­ ence fo r the INT 21h services, the BIOS - Turbo's SP points here services, and other software aspects of the machine.] High addresses Swan, Tom. Mastering Turbo Pascal. Hay­ den, 1986. [A solid survey of Turbo Pascal, Turbo's BP and the instruc tion pointer IIP) of the return address ore al­ including its nonstandard elements. Treats ways as shown. Pass ed parameters are a t higher addresses, with the the subject of this article and much more.] last parameter passed li sted fi rst , a nd so o n. Michael Bertrand teaches mathematics Tu rbo 's doto segment IDS) was saved on the stack only because and programming at Madison Area FILL .COM alters it. This va lue is restored in to DS (pop dsl before return ing Technical College, Madison, Wis. to Turbo . Artwork, Steve Campbell Figure 2.

56 COMPUTE R LANGUAGE • SEPTEMBER 1987