Author Topic: 8 bit multiplication  (Read 7509 times)

Offline tejaswi2195

  • Jr. Member
  • *
  • Posts: 9
8 bit multiplication
« on: February 13, 2014, 03:59:25 PM »
pls find the attachment

i hv written the code for 8 bit multiplication

it gives me the right result in LSB bits
but some unwanted nos also appear
i debugged the code

when i accept num2 wh does value of num1 change????
iam not able to understand.

thanks in advance

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: 8 bit multiplication
« Reply #1 on: February 13, 2014, 06:25:38 PM »
Since you are working with 8-bit values, you shouldn't be treating num1 and num2 as 64-bit values. I don't have a 64-bit machine to work with here, so I can't be sure that it's the only error in your code, but via a cursory viewing, I would ASSume that is your biggest problem. Try these changes:

Code: ("assg6.asm:48") [Select]
movzx rbx, byte [num1]
Code: ("assg6.asm:54") [Select]
movzx rbx, byte [num2]
I hope this helps, I'm not really the expert on 64-bit systems. :P

Regards,
Bryant Keller

About Bryant Keller
bkeller@about.me

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 8 bit multiplication
« Reply #2 on: February 13, 2014, 08:30:56 PM »
Confirm. I do have a 64-bit machine here. I don't really know how to use it (yet), but I have one. A lot of the time I wish I had the 32-bit machine back, but... time marches on...

I found a third error - essentially the same error...
Code: [Select]
add rbx, [num1]
Unfortunately, we don't have an "addzx" instruction. My first attempt to "fix" this was simply to use bl instead of rbx here. This "worked" but of course an 8-bit by 8-bit multiply can go to 16 bits, and any overflow beyond 8 bits would be lost. So I used another register...
Code: [Select]
movzx rcx, byte [num1]
add rbx, rcx

This appears to work, although it isn't well tested. (hmmm... doesn't work well for "num2" = 0! - fix that) Clean up the display a little, add some comments*, and you've got a program. If the pesky user enters "gh" you calculate a nonsense result - okay for a "well behaved user", but some of 'em aren't. You will probably find that you write more code to deal with bogus input than to do the work...

* Over on Stack Overflow a guy named Brendan made the assertion that in assembly language, there are only two possible errors. Either the comment doesn't describe the correct algorithm, or the code doesn't do what the comment said. An interesting viewpoint.

Best,
Frank


Offline tejaswi2195

  • Jr. Member
  • *
  • Posts: 9
Re: 8 bit multiplication
« Reply #3 on: February 14, 2014, 04:16:51 PM »
i can't do

movzx rbx,byte[num1]

bcoz i need to move contents of rbx into num1 not the other way round
i.e mov [num1],rbx

what i hv done is accept no from user convert to ascii which saves result in rbx which iam moving to num1 and num2

i tried doing movzx rcx,[num1]
and then add rbx,rcx

but then it prints result 4 times
i.e if result is FE01
it prints
FE01FE01FE01FE01
(atleast iam not getting garbage value     ;))

and the actual problem is after accepting num2 why does value of num1 change??
for eg when num1= 00000001
num2=00000002

num1 remains 1 till we don't convert num2

but when we do that num1 becomes something like

0000000200000001

why is that??

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: 8 bit multiplication
« Reply #4 on: February 14, 2014, 05:42:12 PM »
In your original program,
If you reserve 9 bytes instead of 3 bytes for num1 and num2, it works.

Code: [Select]
num1 resb 9
num2 resb 9

Bcoz, the below instructions copies 8 bytes data from rbx to memory location pointed by num1 and num2

Code: [Select]
mov [num1],rbx
mov [num2],rbx

Offline tejaswi2195

  • Jr. Member
  • *
  • Posts: 9
Re: 8 bit multiplication
« Reply #5 on: February 19, 2014, 02:49:38 PM »
thanks for the solution