TITLE: Library Calls and Procedures (LibCallProc2.asm) ;------------------------------------------------------------ ; Name: ; ; Filename: LibCallProc2.asm ; ; Project #: Extra Credit 3 ; ; Completed: February 16, 2003 ; ; Description: My ex.credit assembly program. Will demonstrate ; library calls and procedures using problems from ; Assembly Language For Intel-Based Computers,4th ; Ed. by Kip R. Irvine. Problems are #2, #3, #4, ; and #5 on pg. 176. ; ; Reference: Assembly Language For Intel-Based Computers, ; 4th Ed. by Kip R. Irvine ;------------------------------------------------------------ INCLUDE Irvine32.inc .data inputInt byte "Enter a signed integer: ",0 dispInt byte "Displaying the input integers: ",0ah,0dh,0 sumMsg byte "Displaying sum of the input integers: ",0ah,0dh,0 array SDWORD 10 DUP(?) array1 SWORD 10 DUP(?) msgIntro byte "This is My Name's extra credit assembly program and I will",0dh,0ah byte "demonstrate library calls and procedures using questions 2,3,4",0dh,0ah byte "and 5 on page 176.",0dh,0ah,0 msg1 byte "-------------------------------------",0dh,0ah byte "Demonstrating question 2 on page 176.",0dh,0ah byte "-------------------------------------",0dh,0ah,0 msg2 byte "-------------------------------------",0dh,0ah byte "Demonstrating question 3 on page 176.",0dh,0ah byte "-------------------------------------",0dh,0ah,0 msg3 byte "-------------------------------------",0dh,0ah byte "Demonstrating question 4 on page 176.",0dh,0ah byte "-------------------------------------",0dh,0ah,0 msg4 byte "-------------------------------------",0dh,0ah byte "Demonstrating question 5 on page 176.",0dh,0ah byte "-------------------------------------",0dh,0ah,0 .code main PROC ;///////Intro Message///////////////////////////////// mov edx,OFFSET msgIntro ;intro message into edx call WriteString ;display msgIntro call Crlf ;endl call WaitMsg ;pause message call Clrscr ;clear screen call intArray ;print integers input by user call simpAdd1 ;print sum of integers input by user(2) call simpAdd2 ;print sum of integers input by user(3) call intRand ;print 50 random integers between -20 and 20 exit main ENDP ;///////Question 2 on page 176///////////////////////// ;----------------------------------------------------------- intArray PROC Uses ESI EDX ECX EAX ; ;Asks user for input of integers in an array, fills the array, ;then displays the integers that were input. ;Receives: ESI points to the array ; EDX message display ; ECX = size of array ; EAX = receives input integers ;Returns: Integers input by user ;----------------------------------------------------------- mov edx,OFFSET msg1 ;explain message call WriteString ;display msg1 call WaitMsg ;pause message mov ecx,10 ;loop counter mov esi,OFFSET array ;points to array mov edx,OFFSET inputInt ;input integer message L1:;//Loop(Asks For Integer Input In Array) call WriteString ;display string call ReadInt ;read integer into eax call Crlf ;go to next output line mov [esi],eax ;store in array add esi,4 ;next integer loop L1 ;do loop call Clrscr ;clear screen mov edx,OFFSET dispInt ;display integer message call WriteString ;display dispInt mov ecx,10 ;loop counter mov esi,OFFSET array ;points to array L2:;//Loop(Display Integers In Array) mov eax,[esi] ;get integer from array call WriteInt ;display it call Crlf ;endl add esi,4 ;next array position loop L2 ;do loop call WaitMsg ;pause message call Clrscr ;clear screen ret ;return intArray ENDP ;///////Question 3 on page 176///////////////////////// ;----------------------------------------------------------- simpAdd1 PROC Uses ESI EDX ECX EAX ; ;Asks user for input of integers in an array, fills the array, ;calls to sum the array then calls to display the sum of the ;array. ;Receives: ESI points to the array ; EDX message display ; ECX = size of array ; EAX = receives input integers ;Returns: Sum of integers input by user ;----------------------------------------------------------- RUN = 2 ;number of times to run loop mov edx,OFFSET msg2 ;explain message call WriteString ;display msg2 call WaitMsg ;pause message pushad ;save all registers mov ecx,RUN ;loop counter mov esi,OFFSET array1 ;points to array mov dh,10 ;row 10 mov dl,20 ;column 20 mov bh,dh ;store value in bh mov bl,dl ;store value in bl L1:;//Loop mov dh,bh ;mov value back to dh mov dl,bl ;mov value back to dl call Gotoxy ;locate cursor mov edx,OFFSET inputInt;address of the prompt call WriteString ;display string call ReadInt ;read integer into eax call Crlf ;go to next output line mov [esi],eax ;store in array add esi,4 ;next integer inc bh ;increment bh(change row) loop L1 ;do loop call Clrscr ;clear screen L2: popad ;restore all registers call sumArray ;call sumArray procedure call dispSum ;call dispSum procedure call WaitMsg ;pause message call Clrscr ;clear screen ret ;return simpAdd1 ENDP ;///////Question 4 on page 176///////////////////////// ;----------------------------------------------------------- simpAdd2 PROC Uses ESI EDX ECX EAX ; ;Asks user for input of integers in an array, fills the array, ;calls to sum the array then calls to display the sum of the ;array. ;Receives: ESI points to the array ; EDX message display ; ECX = size of array ; EAX = receives input integers ;Returns: Sum of integers input by user ;----------------------------------------------------------- RUN = 3 ;number of times to run loop mov edx,OFFSET msg3 ;explain message call WriteString ;display msg3 call WaitMsg ;pause message pushad ;save all registers mov ecx,RUN ;loop counter mov esi,OFFSET array1 ;points to array mov dh,10 ;row 10 mov dl,20 ;column 20 mov bh,dh ;store value in bh mov bl,dl ;store value in bl L1:;//Loop mov dh,bh ;mov value back to dh mov dl,bl ;mov value back to dl call Gotoxy ;locate cursor mov edx,OFFSET inputInt;address of the prompt call WriteString ;display string call ReadInt ;read integer into EAX call Crlf ;go to next output line mov [esi],eax ;store in array add esi,4 ;next integer inc bh ;increment bh(change row) loop L1 ;do loop call Clrscr ;clear screen L2: popad ;restore all registers call sumArray ;call sumArray procedure call dispSum ;call dispSum procedure call WaitMsg ;pause message call Crlf ;endl call Clrscr ;clear screen ret ;return simpAdd2 ENDP ;///////Question 5 on page 176///////////////////////// ;----------------------------------------------------------- intRand PROC Uses ECX EAX EDX ; ;Prints out 50 random numbers between -20 and +20. ;Receives: EDX message display ; ECX = loop counter for row and columns ; EAX = random integer ;Returns: 50 Random integers between range -20 and +20 ;----------------------------------------------------------- mov edx,OFFSET msg4 ;explain message call WriteString ;display msg4 call WaitMsg ;pause message call Clrscr ;clear screen mov ecx,50 ;loop counter L1:;//Loop mov eax,41 ;random int [0-40] call RandomRange ;call RandomRange from library sub eax,20 ;sub 20, range still length of ;40, but from -20 to +20 call WriteInt ;diplay random integer call Crlf ;endl loop L1 ;do loop call WaitMsg ;pause message call Clrscr ;clear screen ret ;return intRand ENDP ;----------------------------------------------------- sumArray PROC ; ;Calculates the sum of an array of 32-bit integers. ;Receives: ECX size of array ; ESI points to the array, ECX = array size ;Returns: EAX = sum of the array elements ;----------------------------------------------------- mov ecx,RUN ;loop counter mov esi,OFFSET array1 ;points to array push esi ;save ESI, ECX push ecx mov eax,0 ;set the sum to zero L3:;//Loop add eax,[esi] ;add each integer to sum add esi,4 ;point to next integer loop L3 ;repeat for array size L4: pop ecx ;restore ECX, ESI pop esi ret ;sum is in EAX sumArray ENDP ;----------------------------------------------------- dispSum PROC ; ; Displays the sum on the screen ; Receives: EAX = the sum ; Returns: Display edx sum of array ;----------------------------------------------------- mov ecx,RUN ;loop counter mov esi,OFFSET array1 ;points to array mov dh,14 ;row 14 mov dl,20 ;column 20 mov bh,dh ;store in bh mov bl,dl ;store in bl call Gotoxy ;locate cursor mov edx,OFFSET sumMsg call WriteString ;display message inc bh ;increment bh(change row) mov dh,bh ;mov value back to dh mov dl,bl ;mov value back to dl call Gotoxy ;locate cursor call WriteInt ;display eax call Crlf ;endl mov bh,dh ;store in bh mov bl,dl ;store in bl add bh,2 ;add 2 to bh(change row) mov dh,bh ;mov value back to dh mov dl,bl ;mov value back to dl call Gotoxy ;locate cursor ret ;return dispSum ENDP END main