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

Pages: 1-

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: Vieo 2007-05-28 14:02 ID:1pOoiNCK

Yeah, I forgot to mention. It has to be written in x86 Assembly.

Name: Anonymous 2007-05-28 14:07 ID:vHzYO6iR

[b][code]int[/code[/b][code] main[/code]
YOU FUCKING IDIOT

Name: Anonymous 2007-05-28 14:08 ID:vHzYO6iR

int main
YOU FUCKING IDIOT

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

Yeah, yeah. I know you can use int main and put a return at the end.

Anyway, my problem isn't want the C++ code, it's the ASM code. I need help. Why is it when I move the contents of NUM, which is supposed to be a 2, to AX, AX does not have the value of 2?

Name: Anonymous 2007-05-28 14:14 ID:tZ9N6OqS

gcc -S

If you want to make it in assembly, it's easy.
The 'hard' part is to print an integer to the screen. not really hard.

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

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

>>5
also, you can put a return in a void function as well.
fucking piece of shit fuck you fuck

void __f(void) {
return;
}

shoop das whoop also, you dont make sense at all.


mov ax, [NUM]
WORKS.
fK
WTF DO YOU MEAN
FKFf

Name: Anonymous 2007-05-28 14:18 ID:tZ9N6OqS

>>7
code is terrible.

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

>>8

I just tried your suggestion: mov ax, [NUM]

I put the [] brackets around NUM and the contents of AX still comes up as 080C which is 2060 in decimal if I understand it correctly. Where the hell is it coming from?

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

I got rid of the variable QCOUNT by putting a ; in front of it and turning it into a comment. When I re-assembled it and ran the .exe through the debugger, AX now comes up as F01D. WTF? When I changed the 2 to different values, it remained as 080C, but when I omit a variable, only then the value of AX changes?

Name: Anonymous 2007-05-28 14:33 ID:vHzYO6iR

>>11
Hi "Vieo",
I request that you fuck the fuck off.
Thank you.

Name: Anonymous 2007-05-28 14:44 ID:Dfc/Ezvp

Didn't you get the memo? goto considered dangerous!

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

I assembled the same code, but this time using TASM. I ran it through the Turbo Debugger, and AX gets a value of something like 9FFB. I guess that means the problem is with my code.

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

>>13

Yeah. When I was learning C, my professor said no gotos because they make code difficult to ready, etc, etc.

Now I get to ASM and this other professor says that we should use gotos because they make it easier to see how to translate a C program into an ASM program.

Name: Anonymous 2007-05-28 14:48 ID:Heaven

>>13
lol

Name: Anonymous 2007-05-28 15:11 ID:Heaven

>>15
Vieo you're a fucktard.
goto's are jmp's.
jmp's is how computers work. fuc kyou

Name: Vieo 2007-05-28 15:42 ID:1pOoiNCK

LOL. I got it working. I opened up a new file and started fresh. For some reason it works. I don't know why the other one won't. I'm using notepad as and editor. Maybe some sort of character got stuck in the file some where or something like that.

If I get stuck again I will post up so stick around.

Name: Anonymous 2007-05-28 16:19 ID:Heaven

GOTO and GTFO

Name: Anonymous 2007-05-28 17:04 ID:ERJIAIPl

I'm using notepad as and editor
SPLURT! Faceshot!

Name: Anonymous 2009-01-14 13:42

IHBT

Name: Anonymous 2011-01-31 21:32

<-- check em dubz

Name: Anonymous 2011-02-04 11:42

Name: Anonymous 2011-02-04 15:53

Name: tray 2012-03-15 1:25

Name: Anonymous 2012-03-15 13:45

#include <stdlib.h>
#define __max 500
int pool[__max];
int isPrime(int v, int pool_size){
int i;
for(i = 0; i<pool_size; ++i)
if(v % pool[i] == 0)
return 0;
return 1;
}
int main(){
int primes = 1;
pool[0] = 2;
int i = 3;
while(primes < __max){
if(isPrime(i), primes)) pool[primes++] = i;
i += 2;
}
return 0;
}

gcc file.c -S

Name: Anonymous 2012-03-15 13:47

>>26

OPTIMIZED!!!

Name: Anonymous 2012-03-15 14:36

>>1
Why aren't you using the Sieve of Eratosthenes?

Name: Anonymous 2012-03-15 14:52

>>28
He prefers that we use Knuth's algorithm,
Can you fucking read?

Name: Anonymous 2012-03-15 15:21

>>29
Then why the fuck aren't you using that?

Name: Sgt.Kabu⤘젉kiman덃斈 2012-05-29 2:21

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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