News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

buffer?

Started by tinkywinky, September 24, 2005, 05:04:43 PM

Previous topic - Next topic

tinkywinky

Here's a part of an example code in MASM32 (C:\masm32\examples\EXAMPL10\CONSOLE\HASHAPP\PRIMES)
it computes 3512 primes but I don't quite understand it :'(
here's my question:
a. Does 0ffffh/2 mean divide 0ffffh by 2?
b. what does this line mean?
    mov     eax, 00030002h          ; write 2 numbers at one time
c. why does it have to creat a buffer to compute primes?
   what does the creatbuffer_loop for?

btw, is there any function like MOD in MASM32?
thanks for helping me.
start:

    mov     ecx, 0ffffh / 2         ; counter
    mov     edi, offset buffer      ; here: offset for where to put the numbers
    mov     eax, 00030002h          ; write 2 numbers at one time
    mov     esi, edi                ; where to read the numbers from (for the next step)
createbuffer_loop:
    mov     [edi], eax              ; put 2 numbers into the buffer
    add     eax, 00020002h          ; increase the number that gets put in the buffer
    add     edi, 4                  ; adjust the buffer pointer
    dec     ecx                     ; decrease the counter
    jnz     createbuffer_loop       ; counter <> zero? then do the whole thing again


    mov     edi, esi                ; now restore edi for later
    mov     ecx, 0ffffh / 2         ; counter
    xor     ebx, ebx                ; ebx will hold the number of primes
    xor     edx, edx                ; clear high part of edx, because it will be used as pointer

raymond

Quotebtw, is there any function like MOD in MASM32?

MOD is the remainder of a division of two numbers, normally integers. When you divide, using integer instructions, the quotient is given in the AL/AX/EAX register depending on the type of instruction you use, and the remainder is given in the AH/DX/EDX register.

You should read the Intel manual for the requirements of the DIV and IDIV instructions, and fully understand them, if you want to use them without the risk of your program crashing.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

MichaelW

If you are asking about a compile-time MOD operator, see 'Operators' here:

http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ReferenceGuide/Chap_03.htm

eschew obfuscation

Tedd

Quote from: tinkywinky on September 24, 2005, 05:04:43 PM
a. Does 0ffffh/2 mean divide 0ffffh by 2?
Yes. It's calculated when the program is assembled though, not while running.

Quote
b. what does this line mean?
    mov     eax, 00030002h          ; write 2 numbers at one time
The first two primes are 2 and 3. So in the list it will write the (16-bit) words 0002, 0003 -- it writes as dword to do 2 at once (this is the reason for /2 above)

Quote
c. why does it have to creat a buffer to compute primes?
   what does the creatbuffer_loop for?
That depends on the method used to calculate the primes (the rest of the code is missing).
createbuffer_loop fills the buffer with numbers 0002,0003,0004,......,fffe,ffff
The reason for this, I expect, is for a number-sieve. Given a list of numbers, remove all the multiples of 2, then multiples of 3, then 5, then 7, then 11, and so on until you have a remaining list - this is the list of primes.
(2,3,5,7,11 -- are primes. After removing the multiples of 2, the next number after 2 in the list will be a prime, which will be 3; then after removing the 3s, 5 will be the next, and so on..)

Quote
btw, is there any function like MOD in MASM32?
DIV stores its result in eax. It also stores the result of MOD in edx at the same time
No snowflake in an avalanche feels responsible.