What about take any byte of an int in a char variable?
here is;;; file dwordtobyte.asmand a little test
segment .text
global lwordhbyte, lwordlbyte, hwordhbyte, hwordlbyte, bytestodword
lwordhbyte:
;;;
;;; unsigned char lwordhbyte (unsigned int dword)
;;; return a byte containing the 0x0000FF00 bits of dword
;;;
enter 0,0
push ebx
mov eax, [ebp + 8] ; first argument
rol ax, 8
and eax, 0x000000FF
pop ebx
leave
ret
segment .text
lwordlbyte:
;;;
;;; unsigned char lwordlbyte (unsigned int dword)
;;; return a byte containing the 0x000000XX bits of dword
;;;
enter 0,0
push ebx
mov eax, [ebp + 8] ; first argument
and eax, 0x000000FF
pop ebx
leave
ret
hwordlbyte:
;;;
;;; unsigned char hwordlbyte (unsigned int dword)
;;; return a byte contaning the 0x00XX0000 bits of dword
;;;
enter 0, 0
push ebx
mov eax, [ebp + 8]
rol eax, 16 ; 0x00XX0000 -> 0x000000XX
and eax, 0x000000FF ; let only the lower 8 bits on
pop ebx
leave
ret
hwordhbyte:
;;;
;;; unsgined char hwordhbyte (unsigned int dword)
;;; return a byte containing the 0xXX000000 bits of dword
;;;
enter 0,0
push ebx
mov eax, [ebp + 8]
rol eax, 16 ; 0xXX000000 -> 0x0000XX00
rol ax, 8 ; 0x0000XX00 -> 0x000000XX
and eax, 0x000000FF
pop ebx
leave
ret
bytestodword:
;;;
;;; unsgined int bytestodword (unsigned char HH, unsigned char HL
;;; unsigned char LH, unsigned char LL)
;;; return an dword made by its four arguments
;;; in this fashion 0xHHHLLHLL
;;;
enter 0,0
push ebx
; High word
mov ah, [ebp + 8] ; 0x0000XX00
mov al, [ebp + 12] ; 0x000000XX
rol eax, 16 ; 0x0000XXXX -> 0xXXXX0000
; Low word
mov ah, [ebp + 16] ; 0x0000XX00
mov al, [ebp + 20] ; 0x000000XX
pop ebx
leave
ret
/* file driver.c */
#include <stdio.h>
#include <stdlib.h>
/*
* functions from dword2byte.o file
*/
unsigned char lwordhbyte (unsigned int);
unsigned char lwordlbyte (unsigned int);
unsigned char hwordlbyte (unsigned int);
unsigned char hwordhbyte (unsigned int);
unsigned int bytestoword (unsigned char, unsigned char,
unsigned char, unsigned char);
int
main (void)
{
unsigned int num = 0xFF0a5040, /* 255 10 80 64 */
newnum;
unsigned char HH, HL, LH, LL;
HH = hwordhbyte (num);
HL = hwordlbyte (num);
LH = lwordhbyte (num);
LL = lwordlbyte (num);
newnum = bytestodword (HH, HL, LH, LL);
printf ("bytes values of num %d %d %d %d (in decimal)\n",
HH, HL, LH, LL);
printf ("dword value of newnum %x (in hex)\n", newnum);
return 0;
}
Compiling:
nasm -f elf dwordtobyte.asm
gcc -o bytesfun driver.c dwordtobyte.o