Author Topic: moving a word to an array of bytes  (Read 3462 times)

AW

  • Member
  • *****
  • Posts: 2313
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #15 on: May 09, 2017, 11:12:55 PM »
If you return from the main thread, does the process exit?
If there is more than 1 thread it will not.

mineiro

  • Member
  • ***
  • Posts: 450
Re: moving a word to an array of bytes
« Reply #16 on: May 09, 2017, 11:38:16 PM »
hello sir nickc86;
yes, you're thinking right, you can access members of that array by the way you show.
But you can create a structure too, so you dont need calculate position member based on address because assembler can do that job to you. This is good because on a future you can insert or also change members place without need to go to code and change all member array reference by calculating.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

nickc86

  • Regular Member
  • *
  • Posts: 11
Re: moving a word to an array of bytes
« Reply #17 on: May 09, 2017, 11:49:13 PM »
This is for a project at university. I'll be copying this array using movsb, so I don't know that creating a structure for this is the way to go. This array will be going in to another block of memory for later processing. I will have something that looks like this

Code: [Select]
packetsize equ 8
numpackets equ 20
packet byte packetsize dup (?)
queue byte numpackets*packetsize dup (?)
And I'll want to do things like
Code: [Select]
mov eax, OFFSET queue
;get the third byte of the second packet
add eax, packetsize
mov bl, byte ptr [eax + 2]

Or doing this
Code: [Select]
;copy the 3rd packet
mov eax, 3
imul eax, packetsize
mov esi, OFFSET queue
add esi, eax
mov ecx, packetsize
mov edi, OFFSET packet
cld
rep movsb

Would I be going about this in the correct manner?

mineiro

  • Member
  • ***
  • Posts: 450
Re: moving a word to an array of bytes
« Reply #18 on: May 10, 2017, 12:19:48 AM »
yes, you can go that way.

Quote
But I want to access those 8 bytes in the following order:
byte byte byte alignbyte word word

Code: [Select]
my_structure struct
bone db ?
btwo db ?
bthree db ?
pad db ?
wone dw ?
wtwo dw ?
my_structure ends

.data
;...
.code
  mov eax, OFFSET queue
  assume eax:ptr my_structure
  mov [eax].wone,5645
  mov cx,[eax].wone
  assume eax:nothing
  add eax, sizeof my_structure

  assume eax:ptr my_struct_inside_struct
  ...

--edited--
showing other examples:
  add eax,sizeof my_structure*packetsize+numpackets
  add eax, sizeof my_structure*sizeof other_struct*packetsize/sizeof otherstruct
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mineiro

  • Member
  • ***
  • Posts: 450
Re: moving a word to an array of bytes
« Reply #19 on: May 10, 2017, 01:37:46 AM »
hello sir jj2007;

On ms-dos an exitprocess can be just 'ret'. On windows too

On Windows, ret will trigger RtlExitUserThread - not the best option. But it will indeed work most of the time.

See also Raymond Chen's If you return from the main thread, does the process exit?

On ms-dos an exitprocess can be just 'ret'. On windows too, but on linux not, but this is a bad pratice.

int 20h to .com, int 21h to .exe, int 80h on linux 32, syscall on linux 64, ExitProcess on windows 32 and 64...

I'd rather be this ambulant metamorphosis than to have that old opinion about everything

nidud

  • Member
  • *****
  • Posts: 1735
    • https://github.com/nidud/asmc
Re: moving a word to an array of bytes
« Reply #20 on: May 10, 2017, 02:33:44 AM »
Code: [Select]
mov eax, OFFSET queue
;get the third byte of the second packet
add eax, packetsize
mov bl, byte ptr [eax + 2]

Offset is just a static number so you may do it directly:
Code: [Select]
mov bl,queue[packetsize+2]

Code: [Select]
        lea esi,queue[packetsize*3]
lea edi,packet
mov ecx,packetsize
rep movsb

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: moving a word to an array of bytes
« Reply #21 on: May 10, 2017, 06:15:45 AM »
Helllo Nidud,  :t
  :icon14: