News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

division With Remainder Example

Started by hfheatherfox07, January 06, 2013, 12:51:16 AM

Previous topic - Next topic

hfheatherfox07

Hi there can any body show me how to divide 9 by 2 and get 4.5 ?
I get 4 the Remainder  is lost  :(

Thank You !

include \masm32\include\masm32rt.inc

division proto

.data
caption  db "division With Remainder Example", 0
Format   db "%d",0
var1     dd 9h ; binary values
var2     dd 2h ; binary values
buffer   db 32 dup(0)

.data?
finalVal dword  ?
.code

Start:
invoke division
invoke MessageBox, 0, addr buffer, addr caption,  MB_ICONQUESTION + MB_OK
invoke ExitProcess, 0
division Proc

    mov eax, var1
    mov ebx, var2
    sub edx, edx          ;set edx to zero
    div ebx
mov finalVal,eax ; store the result
invoke wsprintf, addr buffer, addr Format, eax

xor eax,eax
ret

division endp
end Start
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

qWord

you must use the FPU (or SSEx) to get floating point results:
LOCAl x:REAL8

fld FP8(9.0)
fdiv FP8(2.0) ; (commonly you would multiply by 0.5)
fstp x

fnc crt_printf,"%.8G\n",x
MREAL macros - when you need floating point arithmetic while assembling!

Dubby

the remainder is inside EDX variable.. which is in this case is 1 (one).


jj2007

Quote from: hfheatherfox07 on January 06, 2013, 12:51:16 AM
Hi there can any body show me how to divide 9 by 2 and get 4.5 ?

- multiply by 5
- convert to string (45)
- move last byte into al (it's 5)
- insert a "." into that position
- move al right after the dot

dedndave

Quote from: qWord on January 06, 2013, 01:55:45 AM
you must use the FPU (or SSEx) to get floating point results:

well - maybe so
but you can do "fixed point" math, as well

if you are always going to be dividing by a power of 2, it's not too difficult to get fast results using shift
binary numbers may have a decimal point, the same as decimal numbers
10101.0101
in this example, the bits before the decimal point represent 16, 8, 4, 2, 1 (decimal)
the bits after the decimal point represent 0.5, 0.25, 0.125, 0.0625 (decimal)

when you use SHR EAX,1 to divide the value in EAX by 2, the 1's bit is shifted into the carry flag
in this case, the carry flag represents the "0.5" bit
it can be recovered into another register and be interpreted as such

if you wanted to use a similar method to divide by 4 or 8 (etc), you could use the SHRD and SHR instructions

qWord

Quote from: dedndave on January 06, 2013, 03:13:58 AMbut you can do "fixed point" math, as well
he can also use his fingers to get  it  :icon_rolleyes:
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

 :biggrin:

hey - we have to giver her options - lol
besides, a little understanding of binary math is a good thing   :t

qWord

Quote from: dedndave on January 06, 2013, 03:18:10 AMhey - we have to giver her options - lol
besides, a little understanding of binary math is a good thing   :t
good to know, but not needed for architectures that comes with instructions for FP arithmetic.
(For me fixed point math is this "Microcontroller and FPAG related stuff")
MREAL macros - when you need floating point arithmetic while assembling!

dedndave


jj2007

Add your toes, and you can arrive at 20 8)

dedndave

20 ? - lol
don't you mean 1111111111.1111111111   :P

i don't believe you have a "handle" on this binary thing, Jochen   :lol:

qWord

If one can live with 10 finger, 5 toes and an implicit limb of precision, he may use one foot as exponent  :P
MREAL macros - when you need floating point arithmetic while assembling!

hfheatherfox07

Quote from: qWord on January 06, 2013, 01:55:45 AM
you must use the FPU (or SSEx) to get floating point results:
LOCAl x:REAL8

fld FP8(9.0)
fdiv FP8(2.0) ; (commonly you would multiply by 0.5)
fstp x

fnc crt_printf,"%.8G\n",x


Hi all and thank you for the replies ....

What I am trying to do is use it when one of the numbers is unknown from a buffer ...
9/2 was just a random example ....
If I use a buffer how can I use use floating-point initializer ?
So 9 instead of 9.0 ???

I could not get qwords example to work with a messageBox Only Console

This is what I want it for
I want the answer here to have 2 decimal reminder

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
MsgCaption      db "PUTER TIME",0
format          db "Your puter has run %d:hours since the last boot, give it a break!",0

.data?
Buffer      db 1024 dup (?)
szHoursVal dword  ?

.code

start:

invoke GetTickCount
   ;Hours
  mov szHoursVal,eax ; store the result of GetTickCount in eax
  mov ebx,3600000
  sub edx, edx          ;set edx to zero
  div ebx
   invoke wsprintf,addr Buffer,addr format, eax
   invoke MessageBox, NULL, addr Buffer, addr MsgCaption, MB_ICONASTERISK

   invoke ExitProcess,NULL
end start
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

hfheatherfox07

I am seeing the same questing posed here :
http://stackoverflow.com/questions/10570453/intel-x86-assembly-collecting-remainders-of-a-division

http://stackoverflow.com/questions/8231882/how-to-implement-the-mod-operator-in-assembly
http://stackoverflow.com/questions/8971627/masm-using-registers-as-expressions-between-mod-operator
I do not get the responses ?

How do you use mod  ?

Thank You
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

anta40

Quote from: hfheatherfox07 on January 07, 2013, 07:21:50 PM
How do you use mod  ?

Thank You

I use div.
Here's the code (FASM syntax, anyway)

format PE console 4.0
entry main

include 'win32a.inc'

section '.data' data readable writeable
num1 dd 27
num2 dd 4
fmt db "%d", 13, 10, 0

section '.code' code readable executable
main:
        xor edx, edx
        mov eax, [num1]
        mov ecx, [num2]
        div ecx
        cinvoke printf, fmt, edx
        invoke ExitProcess, 0
       
section '.idata' import data readable
library kernel32,'kernel32.dll', msvcrt,'msvcrt.dll'
import kernel32, ExitProcess,'ExitProcess'
import msvcrt, printf, 'printf'


It calculates 27 mod 4, which yields 3.