News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Simple Math

Started by ic2, February 20, 2007, 05:49:13 AM

Previous topic - Next topic

ic2

I am trying to find a way where I don't have to use any general registers like eax, ebx, ecx, and edx ...to do some simple additions.  This works with registers.  Is it possible not to use registers and just use buffers or some kind of registers that windows don't mess with when a standard program is running.  I'm thinking Debug Registers but i don't know how to use them so im trying to do it with buffers only after the first run. Thanks in advance for any help

.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
format_2d db "%2d",0
format_lu db "%lu",0
format_lx db "%lx",0

nnum1 db "  ",0
nnum2 db "  ",0

.data?
sum_BUFFER db 20 dup(?)

num1  dd ?
num2  dd ?

numA  dd ?
numB  dd ?
numC  dd ?

.code
start:

;                  ,,,,,,,,,,,,,,   RUN 1
mov eax,12
mov ebx,12
add eax,ebx
    mov numA, eax

invoke wsprintf,addr sum_BUFFER,  addr format_2d,  numA
invoke MessageBox,0,addr sum_BUFFER,0,0

;                  ,,,,,,,,,,,,,,   RUN 2
mov ebx,12
add ebx,numA
    mov numB, ebx

invoke wsprintf,addr sum_BUFFER,  addr format_2d,  numB
invoke MessageBox,0,addr sum_BUFFER,0,0

;                  ,,,,,,,,,,,,,,  NO REGISTER USED HERE
add numB,12
    mov numC, offset numB

invoke wsprintf,addr sum_BUFFER,  addr format_lx, numC
invoke MessageBox,0,addr sum_BUFFER,0,0

invoke wsprintf,addr sum_BUFFER,  addr format_lu, numC
invoke MessageBox,0,addr sum_BUFFER,0,0

invoke ExitProcess,0


hutch--

You can perform adds and subs on memory using an immediate number but the hardware does not support memory to memory adds and subs so you need to use a register for the transfer of data. This is generally why EAX is left for doing tasks like this.


Values loaded in memory.
---------------------------------
mov var1, 1234
mov var2, 5678

mov eax, var1
add var2, eax
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dsouza123

You have plenty of alternatives FPU, MMX, 3DNow, SSE, SSE2 registers.


dsouza123

#3
For example using MMX with the movd and paddd instructions.

.686
.model flat,stdcall
option casemap:none
.mmx

.data?
sum_BUFFER db 20 dup(?)

num1  dd ?
num2  dd ?

numA  dd ?
numB  dd ?
numC  dd ?

.code
start:

mov   num1, 12
mov   num2, 12
movd  mm1, num1
movd  mm2, num2
paddd mm1, mm2
movd numA, mm1

ic2

Thanks hutch-- for the facts.  I been trying endlessly ..

I like MMX. ic now, I'll be using it to doing other things that don't have to interfere with Windows running my program using most or all the General Registers and it's great for Looping the Message System and more...


dsouza123, your example is Excellent.  The idea behind it is exactly what i need to over come a lot of past and future problems.

Thanks Again

jag

Incase you didn't know, Windows only modifies certain general purpose registers.
It will preserve ebx, edi, esi, and ebp.

I use those registers to hold data across api calls rather than using memory since registers are faster.

MMX is really unnecessary for what you want to do in my opinion.

zooba

Also, IIRC, MMX and the FPU are incompatible due to using the same registers. So if an API call uses the FPU, your MMX registers will become rubbish.

Please, correct me if I'm wrong :bg

Cheers,

Zooba :U

ic2

Thanks for the info.   I know the registers are faster but there are some cases where you really out of them.  Are you saying you still use the same registers while CreateWindowsExA and other greedy api is using them all at the same time.  If so do you have an small working example i can get a crash course from. 

QuoteMMX is really unnecessary for what you want to do in my opinion.

Dose it require much.  I don't see it burning up anymore energy than using general registers.  Even Window95 has no problem with MMX code running.  If i'm missing something please let me know???

Funny thing, I did not even know it was called a co-processor like FPU until a masm error said so last night...and now...

QuoteAlso, IIRC, MMX and the FPU are incompatible due to using the same registers. So if an API call uses the FPU, your MMX registers will become rubbish.

So where is MMX, in the cpu or the fpu.  Ooooh sh&t...still got a lot to learn...

Anyway, I'll be using MMX for stuff like dsouza123 code, m32 style procedures and Arrays as i learn MMX.  I plan to leave the rest along for D un-interrupted MessageCenter having registers of it own. That's should help free up something :)  shouldn't it ?

I have my final question under another topic latter.  I see now i better hit the books.  I received a lot of back to back help in these past 4 weeks that woke me up and got me going 24-24.  Because of that I AM () completely organized for what i need to do and will be setting out some serious payback for things i do know about.[/size]

right now I'm in the state of shock

[attachment deleted by admin]

dsouza123

Some good things about MMX.

It can be used to hold 16 dwords, two to a register, using all eight mmx registers
or 32 words (four per register) or 64 bytes (eight per register).

The mmx registers are mm0, mm1, ... mm6, mm7.

Operations can be done in parallel.

.data
  num1 dd    1,  2
  num2 dd  10, 20
  num3 dd    0,  0

.code
   movq mm1, num1
   movq mm2, num2
   paddd mm1, mm2
   movq num3, mm1   ; num3 will hold  11, 22

MMX support is in most CPUs, starting with the Pentium MMX.


Two very usefull information packed links.

MMX Primer
http://www.tommesani.com/MMXPrimer.html

MMX Instruction Set
http://softpixel.com/~cwright/programming/simd/mmx.php

ic2

jag, I tried esi with CreateWindows and it work.  I see what you mean.

dsouza123, Thanks for those links.  It's easier than what i thought.

daydreamer

I must meantion you can perform limited operations completely without regs, using memory only, inc and dec for example can operate on memory and I find its useful for dec mem, instead of dec reg in outer loops

TIMER_event : inc time, dec countdown
example use as counter:inc FPScounter

ic2

I have another math question.

QuoteYou can perform adds and subs on memory using an immediate number but the hardware does not support memory to memory adds and subs so you need to use a register for the transfer of data.

I understand this, especially about memory to memory which is a heart breaker, but how about an immediate number and a value in memory...

Is this possible.  If not by simple adding, are there any other ways to calculate this where the computer understand.

I have to use a buffer (or some kind of memory) to receive a value that will always be  difference and all i need to do is add a solid value of 32 to what ever the buffer receive than copy the ending results to a list.  All and any suggestions will be used no matter how complicate it may be.  This way i will never forget.


PS: Finally, Thanks daydreamer for that suggestion, but i never founded the Tmer_Event.  I want to know all the basic function of math that can be done on a computer.  I know a lot of it simple once you see it.  Would you believe there are very few threads about math to be founded on any Pure ASM Forum.  You could nearly count them by hand  and   most talk FPU.  After reading things over i now want to talk R E G, including inc & dec.


dsouza123

addition and subtraction

.data
   var dd 256

.code
   add var, 32  ; var <-- 288
   sub var, 32  ; var <-- 256
   inv var      ; var <-- 257
   dec var      ; var <-- 256


remainder (only works for a number (divisor) that is a power of 2)

   mov var, 256
   add var, 10
   and var, 15    ; var <- 10   var = var mod 16
   mov var, 1277  ; var <- 1277
   and var, 255   ; var <- 253  var = var mod 256


mod returns the remainder after integer division
like edx after div

   xor edx, edx
   mov eax, 256
   add eax, 10
   mov ecx, 16
   div ecx        ; eax <-- 16, edx <-- 10 (edx is the remainder)


further elaboration:
mod by a power of 2, ie 2^4 = 16, 2^8 = 256
can be done by

   and var,16-1
   and var,256-1

dsouza123

flip all bits and switch sign

   not var     ; one's complement negation
   neg var     ; two's complement negation switch sign positive to negative or reverse


multiplication by a power of 2

   shl var, 3  ; multiply times 8


division by a power of 2

   shr var, 2  ; integer divide by 4