Author Topic: [SOLVED]error: 16 bits arithmetic, wrong number >:(  (Read 7520 times)

Offline djasy3

  • Jr. Member
  • *
  • Posts: 8
[SOLVED]error: 16 bits arithmetic, wrong number >:(
« on: March 02, 2014, 05:22:55 AM »
hello, i'm trying to resolve an equation with 16 bits arithmetic, my program works perfectly when i use 32 bits
i don't understand why, even if i put 1 or 0 as a value, i have a wrong number. here is my equation: X³ -11X² +98X - 24
note: i use scanf to get the value from the user:
Code: [Select]
[...]
calculer:
    mov   ax, word[IntVal]; value from scanf
    mov   [temp], ax ; on store la variable entré par le user dans la variable temporaire
    imul  ax, ax
    imul  ax, [temp]
    jo erreur
    mov   word[IntVal], ax ;on stock le résultat dans IntVal pour le preserver
    xor   ax, ax
    mov   ax, [temp] ;on y remet la valeur de départ pour les prochains calculs
    imul  ax, ax ;eax * eax => X²
    imul  ax, 11
    jo erreur
    sub   word[IntVal], ax
    xor   ax, ax
    mov   ax, [temp] ;on remet la valeur temp dans le eax
    imul  ax, 98
    jo erreur
    add   word[IntVal], ax
    sub   word[IntVal], byte 24; X³ -11x² + 98x - 24
    jmp afficher
    [...]
you'll find the file attached
« Last Edit: March 02, 2014, 02:19:27 PM by djasy3 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: error: 16 bits arithmetic, overflow >:(
« Reply #1 on: March 02, 2014, 06:28:09 AM »
Immediately after the part you show, you "fall through" into "erreur:". I can't believe you did the same thing in the 32-bit version without the same result. Check again.

Your stack manipulation is absolutely horrid. I find it hard to believe it doesn't crash. Pushing a 16-bit register onto a 32-bit stack is "legal", but it's usually a bad idea (leaves the stack badly aligned). Even though you use 16-bit registers to do the arithmetic, your stack is still 32-bit. To clean up the stack after a call, add 4 for each parameter pushed to esp, not 1!

Best,
Frank


Offline djasy3

  • Jr. Member
  • *
  • Posts: 8
Re: error: 16 bits arithmetic, wrong number >:(
« Reply #2 on: March 02, 2014, 07:41:41 AM »
@ frank: thanks, yeah after the part i've shown, i forgot to put the jump, but i did it just after i noticed that !
Although i used the 32 bits registers on the stack, i still have the same, problem: a long, sometimes, negative wrong number !
« Last Edit: March 02, 2014, 08:00:18 AM by djasy3 »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: error: 16 bits arithmetic, wrong number >:(
« Reply #3 on: March 02, 2014, 11:21:08 AM »

16 bits = two bytes = number in range 0 - 65535, signed is half of that range, in both directions +/-.
I think, this task i a quite challenge , because 16 bits and this kind'a equation: X³ -11X² +98X - 24.
If equation is unsigned and we put such a small number as 40 in X, so X³ is already 64000.
So, now try to assign X some number like 656 and then try to solve it with 16 bit arithmetic. :D
Solution for this task depends on, in what range X is expected to vary.
If X is 41, then X³ is already out of unsigned 16 bit number bounds.

Did it worked in 32 bit version? Then just cut everything in half, and it should work.
So, now, after cut, if 16 bit is not working well, then 32 bit also is not working.

Only problem that can be, is, to work out that - multiplication.


Encryptor256's Investigation \ Research Department.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: error: 16 bits arithmetic, wrong number >:(
« Reply #4 on: March 02, 2014, 01:48:18 PM »
That looks much, much better! I see one last lonely 16-bit push:
Code: [Select]
push word [IntVal]

Try making that "dword". I know you only want a 16-bit value there, but we really want to keep the stack 4-byte aligned, and "%d" tells printf to expect 32 bits (unless I'm mistaken). I don't know C well enough to know how to tell printf we want a "signed short" (I think that's what they call it). I imagine there's a way to do it. Because of this, negative results may not work properly. What might work is:
Code: [Select]
movsx eax, word [IntVal] ; sign extend IntVal int eax
push eax
push IShow
call printf
add esp, 8 ; right!

I wish I could test this, but that accursed "multilib" is still thwarting me. I should try to find source for it and do "make install" like the good old days. (grumpy old man rant deleted...)

This "long sometimes negative wrong number" error sounds familiar. I think it's a case of beating printf into submission. I think you're quite close. Much improved, in my opinion (now just get the CPU to agree!).

Best,
Frank


Offline djasy3

  • Jr. Member
  • *
  • Posts: 8
[SOLVED]Re: error: 16 bits arithmetic, wrong number >:(
« Reply #5 on: March 02, 2014, 02:16:29 PM »
 :D thanks frank, this works perfectly