News:

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

byte converter

Started by ragdog, June 21, 2007, 06:46:11 PM

Previous topic - Next topic

ragdog

hi  guys

i look for a algo that convert kilobyte>megabyte>gigabyte

can your help me please

thanks in forward

ragdog

dsouza123

For memory amounts

1 gigabyte == 1024 megabytes
1 megabyte == 1024 kilobytes
1 kilobyte == 1024 bytes

Example

3 gigabytes => 3 * 1024 => 3072 megabytes
5 gigabytes => 5 * 1024 * 1024 => 5242880 kilobytes

7 megabytes => 7 * 1024 => 7168 kilobytes

GregL

I remember it like this:


  kilobyte = 1024

  megabyte = 1024 * 1024

  gigabyte = 1024 * 1024 * 1024



If you follow the IEC and SI it's kibibyte, mebibyte and gibibyte.


evlncrn8

remember some hdd manufacturers also round the 1024 down to 1000

Vortex

ragdog,

The correct bitwise functions will help you to do the necessary convertions.

dsouza123

Vortex is correct.

Here is some code examples that can handle 100s of gigabytes to bytes,
and other smaller conversions, both with hardcoded shift constants
and using cl (ecx) for flexibility.


.686
.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
   giga dd 215, 0
   mega dd  72, 0
   kilo dd   7, 0
   ans  dd   0, 0

.code
start:
   mov  edx, giga+4  ; gigabytes to megabytes
   mov  eax, giga
   shld edx, eax, 10
   shl  eax, 10
   mov  ans+4, edx
   mov  ans+0, eax

   mov  edx, giga+4  ; gigabytes to kilobytes
   mov  eax, giga
   shld edx, eax, 20
   shl  eax, 20
   mov  ans+4, edx
   mov  ans+0, eax

   mov  edx, giga+4  ; gigabytes to bytes
   mov  eax, giga
   shld edx, eax, 30
   shl  eax, 30
   mov  ans+4, edx
   mov  ans+0, eax

   mov  ecx, 10
   mov  edx, mega+4  ; megabytes to kilobytes
   mov  eax, mega
   shld edx, eax, cl
   shl  eax, cl
   mov  ans+4, edx
   mov  ans+0, eax

   invoke ExitProcess, 0
end start

ragdog

thanks to all it´s verynice :U :U

ragdog

ragdog

#7
hi @all


can explain their the registers to me!

   mov    esi,eax 
    shr     esi,10        <<--------- div  esi /1024 ?
   mov    esi,eax
     shl    esi,10 <<--------------- mul esi*1024 ?

I hope that it is correct

my another problem is GetDlgItemInt api get do only 10 signs?
if more than 10 signs, work it not

              invoke GetDlgItemInt, hWnd, 1001,NULL,FALSE
                  test eax,eax
                     jz @kb
                 mov esi,eax
                   shr esi,10
              invoke SetDlgItemInt,hWnd,1002 ,esi,TRUE

can your help me????????
thanks

Tedd

You can use SHL and SHR to replace multiply/divide by a power of 2 (they can be a little faster) -- think about how binary numbers are represented. So "shl eax,10" would be the same as "mul (2^10)" (2^10 = 1024)

"GetDlgItemInt" returns an 'int' -- a dword; the largest decimal value that can be represented in 32 bits (signed) is 2147483647, which is ten digits, so any number larger than this requires more bits.
No snowflake in an avalanche feels responsible.

ragdog

thanks tedd :U

nice for you repley