The MASM Forum

General => The Campus => Topic started by: nickc86 on May 09, 2017, 12:50:54 PM

Title: moving a word to an array of bytes
Post by: nickc86 on May 09, 2017, 12:50:54 PM
I am trying to understand using operands of different sizes
Say I have the following:
array byte 8 dup (?)

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

Just as an example, maybe I want to do the following:
I want to store one char in the first three bytes, I want to store an unsigned word in the 5th and 6th byte and another unsigned word in the 7th and 8th byte.

Am I trying to do things that aren't possible?
Title: Re: moving a word to an array of bytes
Post by: LordAdef on May 09, 2017, 02:22:33 PM
No, it's pretty easy.

Get the address of array into a register
Iterate the register so it points to the place you want

For bytes you access via "Byte Ptr [reg]"
For words "word Ptr [reg]"
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 03:12:29 PM
Quote from: nickc86 on May 09, 2017, 12:50:54 PM
I want to store one char in the first three bytes, I want to store an unsigned word in the 5th and 6th byte and another unsigned word in the 7th and 8th byte.
Am I trying to do things that aren't possible?

This is a way:


.386

.MODEL FLAT, STDCALL
option casemap:none

option dllimport:<msvcrt.dll>
printf PROTO C arg1:Ptr Byte, printlist: VARARG
option dllimport:<kernel32.dll>
ExitProcess   proto STDCALL :dword

Counter=1
Total=8

TmyStruct STRUCT
REPEAT Total
db Counter
Counter=Counter+1
endm
TmyStruct ENDS


.data
message db "byte1 %d byte2 %d byte3 %d word1 %d word2 %d",0

myStruct TmyStruct <>


.code

start proc
INVOKE printf, addr message, byte ptr myStruct[0], byte ptr myStruct[1], byte ptr myStruct[2],word ptr myStruct[4], word ptr myStruct[6]
invoke  ExitProcess, 0
ret
start endp


end start


; Output: byte1 1 byte2 2 byte3 3 word1 1541 word2 2055
Title: Re: moving a word to an array of bytes
Post by: nidud on May 09, 2017, 04:55:42 PM
deleted
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 05:09:41 PM
Quote from: nidud on May 09, 2017, 04:55:42 PM
Just some nit-picks regarding using the C-library functions in msvcer.dll.

These library functions have a startup and exit module, and the exit() function(s) makes a cleanup call to IO and stdio functions. This flushes streams and close any open open files. For this reason the exit() function should be instead of ExitProcess().


.386
.model flat, c
option casemap:none

option dllimport:<msvcrt.dll>
printf proto :ptr sbyte, :vararg
exit proto :dword


It does not apply in this case, when the process ends everything will be released, flushed and closed.
All libraries are notified that the process is closing and are given a chance to clean.
Check by yourself the DLLMain Entry Point.
Title: Re: moving a word to an array of bytes
Post by: nidud on May 09, 2017, 07:04:01 PM
deleted
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 07:42:16 PM
Quote from: nidud on May 09, 2017, 07:04:01 PM
Quote from: aw27 on May 09, 2017, 05:09:41 PM
It does not apply in this case

True, this is just nitpicking and it works fine for small test-snippets like this, so it's just a matter of (in my view good) habit.

The reason is that LIBC is the portable section of the code so the standard string/IO functions may also be in a local library, or a UNIX/Linux version and so on.

C libraries, whatever they are, are just part of the (sometime) hundreds of libraries linked to a program.
They simply are not entitled to any special treatment - even if statically linked.
Title: Re: moving a word to an array of bytes
Post by: nidud on May 09, 2017, 08:40:02 PM
deleted
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 09:31:28 PM
Quote from: nidud on May 09, 2017, 08:40:02 PM
and the DOS/OS2 versions will do different things to exit the program.

I am not concerned with Linux, DOS or OS2 :biggrin:
I don't think anyone really is, because I have seen everybody using ExitProcess all the time in here. I can also assure you that there is no danger at all when we use ExitProcess after any Standard C Library call.
Title: Re: moving a word to an array of bytes
Post by: nidud on May 09, 2017, 09:48:16 PM
deleted
Title: Re: moving a word to an array of bytes
Post by: mineiro on May 09, 2017, 10:22:27 PM
From my tests, and I don't start by hello world program, I start from how to give control back to O.S. or exit function.
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...
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 10:44:29 PM
Quote from: nidud on May 09, 2017, 09:48:16 PM
Yes, me included. The reason I pointed this out in this case was that you included the library for printf, which also includes exit, and then added kernel32 for ExitProcess.

Actually, ExitProcess was not needed, Ret was enough and already there because the first thing I always do is put a Ret .  :lol: And when we use ExitProcess we don't need a Ret because it is never called.  :lol:
Title: Re: moving a word to an array of bytes
Post by: nickc86 on May 09, 2017, 10:47:09 PM
So to store some data there it would be:

.data
myArray byte 8 dup (?)
.code
mov eax, OFFSET myArray
mov word ptr [eax + 6], 5645

And to retrieve:

mov cx, word ptr [eax + 6]

Is this correct?
Title: Re: moving a word to an array of bytes
Post by: jj2007 on May 09, 2017, 10:49:31 PM
Quote from: mineiro on May 09, 2017, 10:22:27 PM
On ms-dos an exitprocess can be just 'ret'. On windows too

On Windows, ret will trigger RtlExitUserThread - not the best option (https://blogs.msdn.microsoft.com/oldnewthing/20150814-00/?p=91811/). 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? (https://blogs.msdn.microsoft.com/oldnewthing/20100827-00/?p=13023)
Title: Re: moving a word to an array of bytes
Post by: nickc86 on May 09, 2017, 10:49:58 PM
Also, the exit process stuff is pretty interesting. Although, I don't understand much of it right now. I am pretty new to masm, as I am sure you can tell  :icon_eek:
Title: Re: moving a word to an array of bytes
Post by: aw27 on May 09, 2017, 11:12:55 PM
Quote from: jj2007 on May 09, 2017, 10:49:31 PM
If you return from the main thread, does the process exit?
If there is more than 1 thread it will not.
Title: Re: moving a word to an array of bytes
Post by: mineiro 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.
Title: Re: moving a word to an array of bytes
Post by: nickc86 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


packetsize equ 8
numpackets equ 20
packet byte packetsize dup (?)
queue byte numpackets*packetsize dup (?)

And I'll want to do things like

mov eax, OFFSET queue
;get the third byte of the second packet
add eax, packetsize
mov bl, byte ptr [eax + 2]


Or doing this

;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?
Title: Re: moving a word to an array of bytes
Post by: mineiro on May 10, 2017, 12:19:48 AM
yes, you can go that way.

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


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
Title: Re: moving a word to an array of bytes
Post by: mineiro on May 10, 2017, 01:37:46 AM
hello sir jj2007;

Quote from: jj2007 on May 09, 2017, 10:49:31 PM
Quote from: mineiro on May 09, 2017, 10:22:27 PM
On ms-dos an exitprocess can be just 'ret'. On windows too

On Windows, ret will trigger RtlExitUserThread - not the best option (https://blogs.msdn.microsoft.com/oldnewthing/20150814-00/?p=91811/). 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? (https://blogs.msdn.microsoft.com/oldnewthing/20100827-00/?p=13023)

Quote from: mineiro on May 09, 2017, 10:22:27 PM
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...

Title: Re: moving a word to an array of bytes
Post by: nidud on May 10, 2017, 02:33:44 AM
deleted
Title: Re: moving a word to an array of bytes
Post by: RuiLoureiro on May 10, 2017, 06:15:45 AM
Helllo Nidud,  :t
  :icon14: