The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on September 26, 2008, 10:58:25 PM

Title: 32Bit to 16/8Bit
Post by: n00b! on September 26, 2008, 10:58:25 PM
Sorry for this noob-question again.
I should already know this but I seem to forgot it...

I want the value of a 32 Bit register in a 16- or 8 Bit register, like:

mov cl, eax

How to do this?
Title: Re: 32Bit to 16/8Bit
Post by: jdoe on September 26, 2008, 11:12:06 PM
Quote from: n00b! on September 26, 2008, 10:58:25 PM
I want the value of a 32 Bit register in a 16- or 8 Bit register, like:

mov cl, eax


Depends on what part of eax you want in cl


mov cl, al
mov cl, ah



shr eax, 16
mov cl, al
mov cl, ah


What you want to do exactly ?

Title: Re: 32Bit to 16/8Bit
Post by: askm on September 26, 2008, 11:14:25 PM
Even the most experienced coders have this problem.

We are going to do this one together.

Lets start by going outside for fresh air,

at least 64 bits of fresh air. Hope that does

not confuse us too much. When we return,

we will align ourselves a bit, and jump right back to it.

Hope this helps us going into the weekend.
Title: Re: 32Bit to 16/8Bit
Post by: jdoe on September 26, 2008, 11:20:25 PM
Quote from: askm on September 26, 2008, 11:14:25 PM
Even the most experienced coders have this problem.
We are going to do this one together.
Lets start by going outside for fresh air,
at least 64 bits of fresh air. Hope that does
not confuse us too much. When we return,
we will align ourselves a bit, and jump right back to it.
Hope this helps us going into the weekend.


I must be very tired (not sleept much this week), but your post is kind of esoteric   :dazzled:

:lol

Title: Re: 32Bit to 16/8Bit
Post by: n00b! on September 26, 2008, 11:25:40 PM
So I cannot move the values directly, but roll the bits inside the register?

PS: I just wanted to do it like this

var1 dd 6
mov al, BYTE ptr [var1]
Title: Re: 32Bit to 16/8Bit
Post by: MichaelW on September 27, 2008, 05:04:18 AM
For memory you can access any byte directly just by using the correct offset.

mov al, BYTE PTR var1     ; least significant byte
mov al, BYTE PTR var1+1
mov al, BYTE PTR var1+2
mov al, BYTE PTR var1+3   ; most significant byte

Title: Re: 32Bit to 16/8Bit
Post by: jj2007 on September 27, 2008, 06:52:42 AM
Quote from: n00b! on September 26, 2008, 10:58:25 PM
I want the value of a 32 Bit register in a 16- or 8 Bit register, like:

mov cl, eax

How to do this?

You need not do anything because your value is already in the 16- or 8-bit register. Example:


include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0

start: mov eax, 1234
MsgBox 0, str$(ax), addr AppName, MB_OK
exit

end start


Now, if you try    MsgBox 0, str$(al), addr AppName, MB_OK, you will see 210, not 1234 - because 1234 does not fit in 8 bits (0...255). But in any case, there are no separate al, ah and ax registers. All three are part of eax, so in the moment when you have loaded eax, the three sub-registers are loaded as well.

I hope that was not too esoteric :dazzled:
Title: Re: 32Bit to 16/8Bit
Post by: dsouza123 on September 27, 2008, 01:03:14 PM
You can only get  8 bits of a 32 bit register into a  8 bit register.
You can only get 16 bits of a 32 bit register into a 16 bit register.


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

    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\user32.lib

.data
  bhold db "0123"
  dhold dd 33323130h ; the 4 bytes in dword contain same as bhold and in the same order

.code
start:
   mov eax, dword ptr bhold  ; copy a dword of data from byte type variable
   mov  cl, al               ; get byte 0, the lo byte, from eax, 48, ascii 0
   mov  cl, ah               ; get byte 1,              from eax, 49, ascii 1
   bswap eax                 ; swap the order of bytes  0123 -> 3210
                             ; highest to lowest, and two middle switch values
   mov  cl, ah               ; get original byte 2,     from eax, 50, ascii 2
   mov  cl, al               ; get original byte 3,     from eax, 51, ascii 3

   mov  cl, byte ptr dhold+0 ; get byte 0 of memory dword
   mov  cl, byte ptr dhold+1 ; get byte 1
   mov  cl, byte ptr dhold+2 ; get byte 2
   mov  cl, byte ptr dhold+3 ; get byte 3

   mov eax, dword ptr bhold
   mov  cx, ax               ; get lo word of eax, 3130h

                             ; the following three sequences get access to hi word
                             ; but have varying side effects
   mov eax, dword ptr bhold
   bswap eax                 ; 33323130h -> 30313233h
   xchg al, ah               ; 30313233h -> 30313332h
   mov  cx, ax               ; get hi word of original eax

   mov eax, dword ptr bhold
                             ; equivalent to previous sequence, but fills carry flag
   ror eax, 16               ; 33323130h -> 31303332h, rotate 16 bits to right, to lo direction
   mov  cx, ax               ; get hi word of original eax

   mov eax, dword ptr bhold
                             ; hi word goes to lo word, new hi word filled with 0 bits
   shr eax, 16               ; 33323130h -> 00003332h, shift  16 bits to right, to lo direction
   mov  cx, ax               ; get hi word of original eax

   invoke  ExitProcess, 0
end start
Title: Re: 32Bit to 16/8Bit
Post by: Mark Jones on September 27, 2008, 01:55:28 PM
All you need to do is study the \MASM32\help\asmintro.chm file.
Title: Re: 32Bit to 16/8Bit
Post by: n00b! on September 27, 2008, 08:14:55 PM
I think I got it, thanks  :U