0
mov eax, [input]
mov ebx, [input2]
imul ebx, eax     

multiply process

mov edx, 0          
mov ecx, ebx
mov ecx, 2h
div ecx             
mov ecx, eax  

division process

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
hazelnut
  • 1
  • 1
  • 1
    Next time, [edit] your question instead of deleting [the old one](https://stackoverflow.com/questions/50663432/how-to-divide-2-input-after-multiplying) and reposting. It didn't even have any downvotes or closevotes yet. – Peter Cordes Jun 03 '18 at 05:23
  • I did, but my internet connection are slow so.. – hazelnut Jun 03 '18 at 05:26
  • And again, use `shr eax,1` to divide by 2. [`div` is horrible for dividing by 2](https://stackoverflow.com/questions/40354978/why-is-this-c-code-faster-than-my-hand-written-assembly-for-testing-the-collat/40355466#40355466). I think your bug is that your multiply result is in `ebx`. Maybe you wanted `imul eax, ebx`? You don't say what result you want your code to give. Currently it gives you `ebx= input2 * input`, and `ecx = input / 2`. Is that not what you want? Use a debugger to single step and look at registers. – Peter Cordes Jun 03 '18 at 05:26
  • i haven't learn that code yet, and my lecturer said to use **div** :/ but thanks anyway :) – hazelnut Jun 03 '18 at 05:32
  • If your lecturer says "use div", you use "div" for what you give back to lecturer, and meanwhile you study full 80586 instruction set (for a start, keep 686+, MMX, SSE, AVX and even floating point for later), try out everything in debugger, and learn how to use also `shr` and why it is lot more efficient way to divide by two (I mean down to the principle level, i.e. that every information in computer is encoded in binary). The point is, that it's highly likely your lecturer knows very little about assembly, and you can push yourself to learn much more than your school offers. Just try (hard). – Ped7g Jun 03 '18 at 05:45
  • 1
    Your second piece of code: `mov ecx, ebx` `mov ecx,2` = why does this set `ecx` twice? Maybe the first one was supposed to be `mov eax,ebx`? Because `div ecx` does divide unsigned 64 bit value in `edx_eax` by unsigned 32 bit value in `ecx`, and you don't mention `eax` in your second piece of code (which may be OK, if you expect already the value to be divided in `eax`). (also you may want to check http://www.cs.virginia.edu/~evans/cs216/guides/x86.html - it's MASM syntax, which needs little fixing for NASM, but you should be able to resolve that by checking NASM docs and debugging a lot). – Ped7g Jun 03 '18 at 05:48
  • Thanks, I'll try (hard) :) – hazelnut Jun 03 '18 at 05:55
  • Are you supposed to be doing `input / input2` instead of dividing by 2? Yes you should use `div` for that, and that would match your question title. But IDK why you'd be using an immediate constant of `2` if you actually wanted to divide to variables. Like I said, [edit] your question to say what you *want* it to do. Otherwise we can't tell which parts are mistakes and which parts are already doing what you want. (Writing to `ecx` twice in a row is obviously some kind of mistake or redundancy, though.) – Peter Cordes Jun 03 '18 at 16:50

0 Answers0