# asm getting bit from dword

Page 1 of 1

## 11 Replies - 1600 Views - Last Post: 21 August 2012 - 03:28 PM

### #1 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

# asm getting bit from dword

Posted 17 August 2012 - 02:49 PM

hee folks

my question is how to get a bit to a buffer. eax contains a dword and i have to get every bit from this dword in a buffer. let me show you a example.

eax = 0000A0B6

buffer 1 should get; A
buffer 2 should get; 0
buffer 3 should get; B
buffer 4 should get; 6

any help?
Is This A Good Question/Topic? 0

## Replies To: asm getting bit from dword

### #2 GunnerInc

• "Hurry up and wait"

Reputation: 897
• Posts: 2,335
• Joined: 28-March 11

## Re: asm getting bit from dword

Posted 17 August 2012 - 04:18 PM

You need to isolate the low byte (4 bits) get it, and rotate right by 4 to get next number.

```    mov     ecx, 16     ; test number is 16 bits, change to 32 for a full DWORD
mov     esi, 0A0B6H ; your test numbewr
GetByte:
mov     eax, esi    ; move number to eax
and     eax, 0FH    ; get least significant byte (4 bits)
; Number is in al now, do what you want with it here.
PrintHex al
sub     ecx, 4      ; decrease our counter
ror     esi, 4      ; rotate the bits in the number right by 4 to get next number
jnz     GetByte     ; is counter 0?  If not, get next number
```

Output:
al = 06
al = 0B
al = 00
al = 0A

### #3 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

## Re: asm getting bit from dword

Posted 18 August 2012 - 03:05 AM

thanks gunnerinc

but actually i want the ascii value, not hex value.

so when al = 06, i want the ascii 6, which is 36 in hex.
another example when al = 0A, i want the ascii A (41h)

is this possible?

### #4 ishkabible

• spelling expret

Reputation: 1624
• Posts: 5,716
• Joined: 03-August 09

## Re: asm getting bit from dword

Posted 18 August 2012 - 09:18 AM

what? I'm not sure your defining this very well. what is it that your trying to do? I'm almost sure there is better way.

### #5 GunnerInc

• "Hurry up and wait"

Reputation: 897
• Posts: 2,335
• Joined: 28-March 11

## Re: asm getting bit from dword

Posted 18 August 2012 - 09:30 AM

See the comment I left in the code

Quote

; Number is in al now, do what you want with it here.

The CPU does cannot tell the difference between hex, dec, oct, ascii or whatever. It sees 0000A0B6 as 1010000010110110 (actually it sees it in Little Endian order), it is up to you to do as you want.

```0  1  2  3  4  5  6  7  8  9 < decimal numbers
48 49 50 51 52 53 54 55 56 57 < ASCII code for numbers
```

See the differece between the number and the ASCII of the number? Now, how would you convert a number to ASCII?

### #6 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

## Re: asm getting bit from dword

Posted 18 August 2012 - 12:01 PM

i hope im able to explain it cleary what i want to do

i got a constant string like 1234567890 and i got some random characters, after doing some math with these random charaters the output is a dword like 0000A0B6.

then i want to replace the output to some positions of the constant string.

here is example

- constant string == 1234567890
- output == 0000A0B6
- replace the output to a constant position of the constante string, like ; A20456B896

### #7 GunnerInc

• "Hurry up and wait"

Reputation: 897
• Posts: 2,335
• Joined: 28-March 11

## Re: asm getting bit from dword

Posted 18 August 2012 - 03:10 PM

0000A0B6 is not a DWORD, it is a STRING. I already said the CPU only knows 1's and 0's, how YOU see it displayed is from a conversion.

What is a "constant" string? I am not up on the High Level terminology. How is this string defined? Data defined as a constant in Assembly CANNOT be changed, you would need to copy this string to a buffer then do what you want with it.

Is this output string always going to be 4 bytes long, or a full 32 bits?

Is the "A", "0", "B", and "6" always going to be in the same insertion point of the string?

The simplest way (the easiest to understand) would be to use 3 pointers.
1. to the buffer to hold the final string
2. to the "constant" string
3. to the "output" string.

We could do it this way, without a loop:
```.data
conststring     db  "1234567890", 0
stringlen       equ (\$ - conststring) - 1

output          db  "A0B6", 0
outputlen       equ  (\$ - output) - 1

.data?
lpBuffer        db  outputlen + stringlen + 1 dup (?)

.CODE
start:
mov     esi, offset conststring
mov     edi, offset lpBuffer
mov     ebx, offset output

; "A"
mov     al, byte ptr [ebx]
mov     byte ptr [edi], al
inc     ebx
inc     edi

; 2
inc     esi
mov     al, byte ptr [esi]
mov     byte ptr [edi], al
inc     esi
inc     edi

; "0"
mov     al, byte ptr [ebx]
mov     byte ptr [edi], al
inc     edi
inc     ebx

; 45
inc     esi
mov     ax, word ptr [esi]
mov     word ptr [edi], ax
inc     edi
inc     edi

; 6
inc     esi
inc     esi
mov     al, byte ptr [esi]
mov     byte ptr [edi], al
inc     edi

; "B"
mov     al, byte ptr [ebx]
mov     byte ptr [edi], al
inc     edi
inc     ebx

; 89
inc     esi
inc     esi
mov     ax, word ptr [esi]
mov     word ptr [edi], ax
inc     edi
inc     edi

; "6"
mov     al, byte ptr [ebx]
mov     byte ptr [edi], al

lea     edi, lpBuffer

push    0
call    ExitProcess
```

Ouput:
edi = A20456B896

### #8 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

## Re: asm getting bit from dword

Posted 18 August 2012 - 04:17 PM

oky string then instead of dw

constant string means not changeable, defined as conststr db "1234567890",0

the output is not always 4 bytes long, but for now to keep it easy, its yes, always 4 bytes long.

yes its always the same insertion point of the string.

im gonna try this code and let you know if i can solved the problem

thank you sir again

### #9 GunnerInc

• "Hurry up and wait"

Reputation: 897
• Posts: 2,335
• Joined: 28-March 11

## Re: asm getting bit from dword

Posted 18 August 2012 - 04:51 PM

Strings in the .data section (or any data) can be modified as you would data in the .data? section.

Strings and data defined in the .const section CANNOT be modified, if you try your program will segfault.

### #10 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

## Re: asm getting bit from dword

Posted 20 August 2012 - 03:16 PM

for a unkown reason when i use your code the winasm studio will crash. im sure that i corrected all lines. i can compile it succesfully.

and i dont understand why you using StrLen, i guess to locate the position in which the byte should be replaced from the constant string?
if so then i think that code you posted with strLen is not needed because the positions are constant so will not change.

1 , 3, 7 ,9 are the positions, so i think its better to write the code for replacing like this:

lea edi, offset ConStr ; load 1234567890
mov al, byte ptr [ebx] ; A
mov byte ptr [edi+1], al
mov al, byte ptr [ebx] ; 0
mov byte ptr [edi+3], al
mov al, byte ptr [ebx] ; B
mov byte ptr [edi+7], al
mov al, byte ptr [ebx] ; 6
mov byte ptr [edi+9], al

### #11 Latias

Reputation: 0
• Posts: 73
• Joined: 28-June 12

## Re: asm getting bit from dword

Posted 21 August 2012 - 03:25 PM

figured out now it works ! thanks you very much sir! you are a great contribution to asm language

### #12 GunnerInc

• "Hurry up and wait"

Reputation: 897
• Posts: 2,335
• Joined: 28-March 11

## Re: asm getting bit from dword

Posted 21 August 2012 - 03:28 PM

Sometime the best answer is to not answer at all :-) You learn how to do it on your own! Feels better to, no? Glad you are starting to understand it!!!