Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Could use a little help...

Name: Vieo 2007-05-28 14:02 ID:1pOoiNCK

I have to hand in a prime number generator that will generate the first 500 primes as a project for school. It's due tomorrow, but he's giving us until Wednesday.

He prefers that we use Knuth's algorithm, but since I don't really get it, I want to try the brute force method. I wrote it in C++, then rewrote it with gotos to make it easier to translate into assembly.

Here it is:
==========================================
#include <iostream.h>

void main()
{
    int max=1000, n=2, i=0, j=0, count=0;

WLOOP: i = n;

FLOOP: if(n % i == 0)
           count++;
       i--;
       if(i >= 1)
           goto FLOOP;

       if(count == 2)
       {
           cout<<n<<" is prime."<<endl;
           j++;
       }
       n++;
       count = 0;

       if(j != max)
           goto WLOOP;
}
============================================

It generate's the 1,000th prime as 7919 so I know it works.


NOW, my problem lays in translating it into assembly. WE WERE supposed to use MASM, but for like the first half of the semester, we were stuck in a room that ran linux machines so we just messed around with MIXAL until I found a copy of MASM and gave it to the professor.

Anyway, the problem I'm having is this damn piece of shit is not working the way it's supposed to.

PROBLEM: For some reason when I move NUM to the AX register, even though I defined NUM as 2, SOME OTHER RANDOM SHIT appears in the AX register.

=====================================
        .MODEL SMALL
        .586
        .STACK 100H
        .DATA

MAX        DW 5
NUM         DW 2
INDEX        DW 0
MCOUNT    DW 0
QCOUNT    DW 0

MSG        DB    'EQUAL', 13, 10, '$'
MSG2        DB    'NOT EQUAL', 13, 10, '$'

        .CODE
MAIN        PROC


                MOV AX, NUM
================================================


See that code snippet above? What am I doing wrong. I am using Microsoft's CODEView debugger to follow what happens after each line is executed.

When MOV AX, NUM is run AX = 080C -- which is not a 2 !


Here is a screenshot: http://img408.imageshack.us/img408/8076/gdamnitlt7.jpg

Please help.

Name: Anonymous 2007-05-28 14:16 ID:sLAE00eE

primes.c:

#include <stdio.h>
#include <math.h> //Should be maths.h

int main() {
    for (int i = 1; i < 500; i++) {
        int prime = 1;
        int limit = floor(sqrt(i)) + 1;
        for (int j = 2; j < limit; j++) {
            if (!(i % j)) {
                prime = 0;
                break;
            }
        }
        if (prime) {
            printf("%d\t", i);       
        }
    }
    printf("\n");
}



gcc -S -O0 -std=c99 -masm=intel primes.c

    .file    "primes.c"
    .intel_syntax
    .def    ___main;    .scl    2;    .type    32;    .endef
    .section .rdata,"dr"
LC1:
    .ascii "%d\11\0"
LC2:
    .ascii "\12\0"
    .text
.globl _main
    .def    _main;    .scl    2;    .type    32;    .endef
_main:
    push    ebp
    mov    ebp, esp
    sub    esp, 40
    and    esp, -16
    mov    eax, 0
    add    eax, 15
    add    eax, 15
    shr    eax, 4
    sal    eax, 4
    mov    DWORD PTR [ebp-24], eax
    mov    eax, DWORD PTR [ebp-24]
    call    __alloca
    call    ___main
    mov    DWORD PTR [ebp-4], 1
L2:
    cmp    DWORD PTR [ebp-4], 499
    jg    L3
    mov    DWORD PTR [ebp-8], 1
    fild    DWORD PTR [ebp-4]
    fstp    QWORD PTR [esp]
    call    _sqrt
    fstp    QWORD PTR [esp]
    call    _floor
    fld1
    faddp    st(1), st
    fnstcw    WORD PTR [ebp-18]
    movzx    eax, WORD PTR [ebp-18]
    or    ax, 3072
    mov    WORD PTR [ebp-20], ax
    fldcw    WORD PTR [ebp-20]
    fistp    DWORD PTR [ebp-12]
    fldcw    WORD PTR [ebp-18]
    mov    DWORD PTR [ebp-16], 2
L5:
    mov    eax, DWORD PTR [ebp-16]
    cmp    eax, DWORD PTR [ebp-12]
    jge    L6
    mov    edx, DWORD PTR [ebp-4]
    lea    ecx, [ebp-16]
    mov    DWORD PTR [ebp-24], ecx
    mov    eax, edx
    mov    ecx, DWORD PTR [ebp-24]
    cdq
    idiv    DWORD PTR [ecx]
    test    edx, edx
    jne    L7
    mov    DWORD PTR [ebp-8], 0
    jmp    L6
L7:
    lea    eax, [ebp-16]
    inc    DWORD PTR [eax]
    jmp    L5
L6:
    cmp    DWORD PTR [ebp-8], 0
    je    L4
    mov    eax, DWORD PTR [ebp-4]
    mov    DWORD PTR [esp+4], eax
    mov    DWORD PTR [esp], OFFSET FLAT:LC1
    call    _printf
L4:
    lea    eax, [ebp-4]
    inc    DWORD PTR [eax]
    jmp    L2
L3:
    mov    DWORD PTR [esp], OFFSET FLAT:LC2
    call    _printf
    mov    eax, 0
    leave
    ret
    .def    _printf;    .scl    3;    .type    32;    .endef
    .def    _sqrt;    .scl    3;    .type    32;    .endef
    .def    _floor;    .scl    3;    .type    32;    .endef

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List