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

nickc86

  • Regular Member
  • *
  • Posts: 11
moving a word to an array of bytes
« 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?

LordAdef

  • Member
  • ****
  • Posts: 642
Re: moving a word to an array of bytes
« Reply #1 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]"

AW

  • Member
  • *****
  • Posts: 2318
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #2 on: May 09, 2017, 03:12:29 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:

Code: [Select]
.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

nidud

  • Member
  • *****
  • Posts: 1735
    • https://github.com/nidud/asmc
Re: moving a word to an array of bytes
« Reply #3 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().

Code: [Select]
.386
.model flat, c
option casemap:none

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

AW

  • Member
  • *****
  • Posts: 2318
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #4 on: May 09, 2017, 05:09:41 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().

Code: [Select]
.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.

nidud

  • Member
  • *****
  • Posts: 1735
    • https://github.com/nidud/asmc
Re: moving a word to an array of bytes
« Reply #5 on: May 09, 2017, 07:04:01 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.

AW

  • Member
  • *****
  • Posts: 2318
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #6 on: May 09, 2017, 07:42:16 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.

nidud

  • Member
  • *****
  • Posts: 1735
    • https://github.com/nidud/asmc
Re: moving a word to an array of bytes
« Reply #7 on: May 09, 2017, 08:40:02 PM »
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.
:biggrin:

Well, if you want to build a Linux version of JWasm the only thing you have to do is to use another make file which uses a different library and recompile the same source code.

The Linux version of exit() will then do something like this:
Code: [Select]
    mov eax, SYS_EXIT
    int 80h

The Windows version will use ExitProcess(), and the DOS/OS2 versions will do different things to exit the program. If ExitProcess() was used in the source you have to change the source code for each version.

The same thing apply to other modifications. If you want to sprite up the strlen() and other functions you could name them my_fast_SSE2_strlen(), but then you have to rename a few thousand names in the source.

But then again, you don't have think about this if you don't want to, but the point is that some of these libraries are what we call standard C libraries and they use standard names like printf, strlen, exit, and so on.

AW

  • Member
  • *****
  • Posts: 2318
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #8 on: May 09, 2017, 09:31:28 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.

nidud

  • Member
  • *****
  • Posts: 1735
    • https://github.com/nidud/asmc
Re: moving a word to an array of bytes
« Reply #9 on: May 09, 2017, 09:48:16 PM »
I am not concerned

 :biggrin:

It wasn't directed to you personally but for people who read this forum to learn programming in general.

Quote
because I have seen everybody using ExitProcess all the time in here.

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.

mineiro

  • Member
  • ***
  • Posts: 450
Re: moving a word to an array of bytes
« Reply #10 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...
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

AW

  • Member
  • *****
  • Posts: 2318
  • Let's Make ASM Great Again!
Re: moving a word to an array of bytes
« Reply #11 on: May 09, 2017, 10:44:29 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:

nickc86

  • Regular Member
  • *
  • Posts: 11
Re: moving a word to an array of bytes
« Reply #12 on: May 09, 2017, 10:47:09 PM »
So to store some data there it would be:
Code: [Select]
.data
myArray byte 8 dup (?)
.code
mov eax, OFFSET myArray
mov word ptr [eax + 6], 5645
And to retrieve:
Code: [Select]
mov cx, word ptr [eax + 6]
Is this correct?

jj2007

  • Member
  • *****
  • Posts: 9687
  • Assembler is fun ;-)
    • MasmBasic
Re: moving a word to an array of bytes
« Reply #13 on: May 09, 2017, 10:49:31 PM »
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?

nickc86

  • Regular Member
  • *
  • Posts: 11
Re: moving a word to an array of bytes
« Reply #14 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: