# C++ & Assembler



## Yoko Beaumont (Nov 23, 2007)

I know this is way off from FurAffinity ,but I am in need of major help with this. Can anyone help me at all? I'll post t he problem when I know someone can help.


----------



## Rilvor (Nov 23, 2007)

Moved to Tech Discussion where it'll get the attention it needs


----------



## Tachyon (Nov 23, 2007)

How are we expected to know whether or not we can help if you won't tell us what the problem is?


----------



## DragonTrew (Nov 23, 2007)

What is the problem at all? Maybe I could help and don't know....


----------



## Yoko Beaumont (Nov 23, 2007)

Okay I'll explain it now. 

; prime.asm
; sample program to demonstrate procedures
; calulates and prints prime numbers from 1 to 20

include 'emu8086.inc'

org  100h ; set location counter to 100h

jmp CodeStart

DataStart:
   max dw 20
   space db " ", 0

CodeStart:
   mov bx, 1

   LoopStart:

       ; must be a prime
       mov ax, bx
       call print_num

       ; print a space
       mov si, offset space
       call print_string

       add bx, 1
       cmp bx, max

   jle LoopStart

   ret

   IsPrime PROC
       ; uses a loop to determine if number in bx is prime
       ; upon return if bx not prime dx will be 0, otherwise dx > 0

       ; we only have to test divisors from 2 to bx/2

       ; prepare to divide dx:ax / 2
       mov ax, bx        
       mov dx, 0
       mov cx, 2 
       div cx

       ; move result into si for loop
       mov si, ax

       ; assume the value is prime
       mov dx, 1

       ; start loop at 2
       mov cx, 2

       PrimeLoop:

           ; compare loop count(in cx) and max loop value (in si)
           cmp cx, si

           ; jump out of loop if count(cx) > si
           ja StopLabel

           ; divide test value (in bx) by loop count (in cx)
           mov ax, bx
           mov dx, 0           
           div cx

           ; check remainder (in dx), if zero then we found a divisor
           ; and the number cannot be prime
           cmp dx, 0

           ; if dx = 0 then we found a divisor and can stop looking
           je StopLabel

           ; increment count
           add cx, 1

       jmp PrimeLoop

       StopLabel:

       ret
   IsPrime ENDP

DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

"Modify the program so that it correctly prints only the prime numbers in the range 1 to 20."

I've never learned how to do this. >.<


----------



## DragonTrew (Nov 23, 2007)

Prime numbers are those who are only divisible by one and itself that the result is not a fraction.

I would: create an array with the numbers to be tested: int numbers[20];
then populate the array:
for(int i=0; i <=20; i++){
  numeros_ = i;
}
then make a method to compare if the number is divisible by 1 or itself
bool isPrimeNumber(double x){
  if(x/x == 1,00)
    return true
  else return false
}

and using this simple method compare all numbers of the array and print them 

Sorry for my bad English and also for any code-fails or something..._


----------



## DragonTrew (Nov 23, 2007)

correction:

bool isPrimeNumber(double x){
if(x/x == 1,00)
return true
return false
}


----------



## MadPlumber (Nov 23, 2007)

That's not C++.  I'm still not understanding what you're asking.


----------



## Yoko Beaumont (Nov 23, 2007)

It' C++ Assembly language, according to TerrorBite. It has to be modified (Using Assembly Language) to spit out all prime numbers between 1 & 20.


----------



## MadPlumber (Nov 23, 2007)

You are probably right, that looks closer to Assembly than it does C++.  However, it seems that you are asking others to write the program for you.  You need to isolate what exactly your dilemma is, whether it is a certain part of code or language.  Simply presenting your assignment to a forum for someone else to write the program for you is not learning.


----------



## Yoko Beaumont (Nov 23, 2007)

You need to learn as well, that some people are asking for -HELP-, before you throw that kind of comment around.


----------



## net-cat (Nov 23, 2007)

Looks to me like you're given the main loop that counts from 1-20 and a procedure that can determine if a number is prime.

I don't know what assembler/linker you're using, so this is a general solution.

Basically, you're going to have to add a label to your "IsPrime" procedure and call it, saving the registers that IsPrime will overwrite. Something like this...

```
push eax
push ebx
push [whatever registers you need to save]
call IsPrime
pop [whatever registers you pushed in reverse order]
```

Then you'll want to jump around the code to print out the number if dx=0

```
cmp edx, 0
je dont_print

; code that prints out character

dont_print:
```

Hope that helps...


----------



## Yoko Beaumont (Nov 23, 2007)

EMU8086. It's the DeVry standard.


----------



## net-cat (Nov 23, 2007)

Ugh. Not about to download a proprietary emulator...

But yeah. My solution basically stands. Drop the "e" from all the register names.


----------



## Eevee (Nov 23, 2007)

DragonTrew said:
			
		

> bool isPrimeNumber(double x){
> if(x/x == 1,00)
> return true
> return false
> }


Um.

Might want to..  check that again.


----------



## net-cat (Nov 23, 2007)

Of course, it would probably be unethical to just write it in C and use "gcc -S"...


----------



## Rostam The Grey (Nov 23, 2007)

I used to check for primes similar to this:

bool IsPrime(double x)
{
double counter = (double)((int)(x / 2));
bool prime = false;
while (!prime && counter > 1)
{
double ans1 = x / counter;
double ans2 = (double)((int) x / counter)
if(ans1 == ans2) prime = true;
counter--;
}
return prime;
}

I tried to do that in C++ but I haven't used C++ in 4 years so I might be a little off. That's probably not the most efficient way to get the answer but I never found a better one.


----------



## Eevee (Nov 23, 2007)

```
sub prime { (1 x shift) !~ /^1?$|^(11+?)1+$/ }
```


----------



## Yoko Beaumont (Nov 23, 2007)

Okay I'm gonna try the Assembler one, not the C code one...Wish me luck!


----------



## Pi (Nov 23, 2007)

Eevee said:
			
		

> ```
> sub prime { (1 x shift) !~ /^1?$|^(11+?)1+$/ }
> ```




```
class Integer
    def divisors
        res=[1]
        2.upto(Math.sqrt(self).floor) do |i|
            if self % i == 0
                res << i
            end
        end
        res.reverse_each do |i|
            res << self / i
        end
        res.uniq
    end
    def prime?
        self.divisors.size == 2
    end
end
```
also

```
Â« DUP SQRT FLOOR
   2 SWAP
   FOR N
        IF DUP N MOD 0 ==
        THEN
            "NP " N + HALT
        END
   NEXT
   "P"
Â»
```


----------



## net-cat (Nov 23, 2007)

Eevee: ... how does that work?


----------



## Yoko Beaumont (Nov 23, 2007)

net-cat said:
			
		

> Looks to me like you're given the main loop that counts from 1-20 and a procedure that can determine if a number is prime.
> 
> I don't know what assembler/linker you're using, so this is a general solution.
> 
> ...



I'm confused at the pop one...

And really all of it...


----------



## Pi (Nov 23, 2007)

Yoko Beaumont said:
			
		

> I'm confused at the pop one...
> 
> And really all of it...



.. How much(little) assembly language work have you done?


----------



## Yoko Beaumont (Nov 23, 2007)

VERY little. I was barely able to do last week's.


----------



## DragonTrew (Nov 23, 2007)

Ohhh, you wanna it in assembly... that chance things...

I'm afraid that I can't help much in that case...


----------



## net-cat (Nov 24, 2007)

Yoko Beaumont said:
			
		

> VERY little. I was barely able to do last week's.


Erf. Yeah, sorry.

I'm happy to answer "what does this do" or "how would I do [small subset]" but I'm really not about to sit down and do the whole assignment for you...


----------



## Yoko Beaumont (Nov 24, 2007)

Didn't ask you to, Net-Cat...I'm just not sure I'll be able to pass this class...


----------



## Pi (Nov 24, 2007)

Yoko Beaumont said:
			
		

> Didn't ask you to, Net-Cat...I'm just not sure I'll be able to pass this class...



Yeah, you sound pretty hosed. Study harder.


----------



## Yoko Beaumont (Nov 24, 2007)

Pi said:
			
		

> Yoko Beaumont said:
> 
> 
> 
> ...



No one asked you for such a comment.


----------



## Pi (Nov 24, 2007)

Yoko Beaumont said:
			
		

> Pi said:
> 
> 
> 
> ...



Actually, *you* did, when you asked for help with your assignment.

Sorry that we're not going to do your work for you. It's not our fault that you can't pass your class, and since you don't know even the basics of assembly-language programming, which you are ostensibly PAYING this college to learn, it's evident that there's something wrong. Since it's unlikely that the university of your choice is going to give an assignment and teach you absolutely nothing as to how to complete it, I choose to invoke Occam's Razor, and note the simpler solution is that you don't know what you're doing and need to learn on your own.

The truth hurts.


----------



## Eevee (Nov 24, 2007)

net-cat said:
			
		

> Eevee: ... how does that work?


Construct a string from a single character repeated n times.

There are three cases: prime, composite, neither.  Test to see if it's composite or neither; if the test fails, it is prime.

"Neither" means 0 or 1; this is /^1?$/.

Composite can be matched by any series of the character -- (11+?) -- more than once occurring more than one time -- 1+.

The performance is abysmal due to all the backtracking, but it is technically correct and will run in finite time.



			
				DragonTrew said:
			
		

> Ohhh, you wanna it in assembly... that chance things...
> 
> I'm afraid that I can't help much in that case...


Did you not see the pasted code?


----------



## net-cat (Nov 24, 2007)

Yeah, I kind of wondered about that. A regular expression didn't seem like the most efficient way to do it...


----------



## DragonTrew (Nov 25, 2007)

Eevee said:
			
		

> DragonTrew said:
> 
> 
> 
> ...



I saw a bunch of codes...what one you're telling me to see..

Also my code to find primes don't work... hehehehe :roll:


----------



## leeter (Nov 25, 2007)

Yoko it seems that your doing x86 assembly, which is definitely not the easiest to learn on my recommendation would be to do the assignment in this order (this is how I do my computers as components assignments which are in asm68000)

1. figure out what actually needs to be done, this is actually the hardest part everything else is just looking out of your book to make sure you're using instructions correctly.
1.a : write out the algorithm in a language you feel comfortable with this does not have to be a programing language.
1.b : break down your algorithm in to component steps making note of where the conditionals are , i.e anywhere the flow of the logic changes. these will become your jump points.
1.c : figure out what data you need and where you need it this is critical for implementation.

2 : either use your book to figure out the critical instructions or google x86 assembly tutorial either will work.

3: allot about 8 hrs to debug, bring paper with you to diagram logic flow this usually helps alot


----------



## Pi (Nov 25, 2007)

leeter said:
			
		

> Yoko it seems that your doing x86 assembly, which is definitely not the easiest to learn on my recommendation would be to do the assignment in this order (this is how I do my computers as components assignments which are in asm68000)



He's got to do x86 asm for a class, or so he's claiming. But the thing is, he's got the hard work DONE for him --- his college gave him an isPrime routine, he's just got to call it 20 times.


----------



## leeter (Nov 25, 2007)

Lucky, I'm just finishing up interrupts and they didn't give us squat (google http://ece-www.colorado.edu/~ecen2120/syllabus.html for more we just finished up assignment 11) mind we've been building on previous assignments.


----------

