; This is the source for the Squares function ; ; Written for Borland Tasm 5.2b which comes with C++Builder. ; ; George Cross, Borland C++ Tech Support, October 1998 ; .386 model flat nosmart locals extrn Itoa:proc extrn PrintString:proc extrn PrintCRLF:proc .code ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Squares ; prints the squares of a contiguous sequence of integers ; without using multiplication. The largest number for which ; this function can find the square is 0b400h (2^15 or so) as ; this function only handles signed integers. ; ; We accomplish this by adding an integer to itself as many times ; as itself. Eg, 5 squared = 5+5+5+5+5 ; ; inputs ; eax integer to begin the sequence ; ecx integer specifying number of contiguous integers in sequence ; ; outputs ; none ; public Squares Squares proc push eax ; save start integer in a local var dec dword ptr [esp] ; decrement to start off loop @@1: push ecx ; save sequence count in a local var inc dword ptr [esp+4] ; increment integer to square mov eax,[esp+4] ; put it in eax mov ecx,eax ; ecx is our loop counter xor ebx,ebx ; ebx is where we will accumulate result @@2: add ebx,eax loopnz @@2 mov eax,ebx ; prepare for Itoa call Itoa ; string returned in eax push eax ; save address of string call StringLength ; find length of string returned in eax mov esi,eax ; PrintString needs length in esi and pop eax ; address of string eax call PrintString call PrintCRLF pop ecx ; retrieve length of sequence loopnz @@1 ; continue if not zero pop eax ; clear off stack ret endp ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; StringLength ; determines the length of the string in EDI ; ; Input ; eax address of the string ; ; Output ; eax length of string ; edi end of string ; StringLength proc cld mov edi,eax xor al,al mov ecx,-1 @@1: inc ecx scasb jnz @@1 mov eax,ecx ret endp end