The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on June 21, 2007, 06:46:11 PM

Title: byte converter
Post by: ragdog on June 21, 2007, 06:46:11 PM
hi  guys

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

can your help me please

thanks in forward

ragdog
Title: Re: byte converter
Post by: dsouza123 on June 21, 2007, 08:39:34 PM
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
Title: Re: byte converter
Post by: GregL on June 22, 2007, 02:26:34 AM
I remember it like this:


  kilobyte = 1024

  megabyte = 1024 * 1024

  gigabyte = 1024 * 1024 * 1024



If you follow the IEC and SI it's kibibyte (http://en.wikipedia.org/wiki/Kibibyte), mebibyte and gibibyte.

Title: Re: byte converter
Post by: evlncrn8 on June 22, 2007, 09:08:53 AM
remember some hdd manufacturers also round the 1024 down to 1000
Title: Re: byte converter
Post by: Vortex on June 22, 2007, 05:40:42 PM
ragdog,

The correct bitwise functions will help you to do the necessary convertions.
Title: Re: byte converter
Post by: dsouza123 on June 22, 2007, 09:14:50 PM
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
Title: Re: byte converter
Post by: ragdog on June 23, 2007, 03:54:31 AM
thanks to all it´s verynice :U :U

ragdog
Title: Re: byte converter
Post by: ragdog on June 24, 2007, 11:02:05 AM
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
Title: Re: byte converter
Post by: Tedd on June 27, 2007, 08:48:35 PM
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.
Title: Re: byte converter
Post by: ragdog on June 27, 2007, 09:09:29 PM
thanks tedd :U

nice for you repley