flat assembler
Message board for the users of flat assembler.
Index
> Main > Concatenate a string, and return it in EAX |
Author |
|
NEASM 22 Aug 2018, 01:39
(i haven't found a good manner to do this in C as well)
|
|||
22 Aug 2018, 01:39 |
|
revolution 22 Aug 2018, 04:22
One thing to realise is that the register EAX is 32-bits wide, so it can only hold a maximum of 4 bytes. So assuming you want just 4 bytes you can do this:
Code: mov ecx,'hel' ;ECX=0x006c6568 mov edx,'l' ;EDX=0x0000006c ;... mov eax,edx ;EAX=0x0000006c shl eax,24 ;EAX=0x6c000000 or eax,ecx ;EAX=0x6c6c6568 = 'hell' |
|||
22 Aug 2018, 04:22 |
|
redsock 22 Aug 2018, 10:00
lest we forget AMD64 and do the same:
Code: mov ecx, 'hel' mov edx, 'lo' mov eax, edx shl rax, 24 or rax, rcx |
|||
22 Aug 2018, 10:00 |
|
NEASM 22 Aug 2018, 10:42
revolution wrote: One thing to realise is that the register EAX is 32-bits wide, so it can only hold a maximum of 4 bytes. So assuming you want just 4 bytes you can do this: Thanks. But, for concatenate strings like this? Code: buffer db 'Hello', 0 buffer2 db ', World!', 0 buffer3 rb 100 ..... mov ecx, [buffer] mov edx, [buffer2] ..... ; something mov [buffer3], eax |
|||
22 Aug 2018, 10:42 |
|
DimonSoft 22 Aug 2018, 12:47
NEASM wrote: Thanks. But, for concatenate strings like this? First describe how would you store the resulting string in 4 bytes. |
|||
22 Aug 2018, 12:47 |
|
Walter 22 Aug 2018, 16:54
The C runtime functions can be used if you are using pointers to strings.
Code: ;************** ;* Concat.asm * ;************** format pe console 4.0 entry start include 'win32a.inc' section '.data' data readable writeable strPart1 db 'Hel',0 strPart2 db 'lo.',0 strFormat db '%s',13,10,0 strResult rb 128 section '.code' code readable executable start: cinvoke strcpy,strResult,strPart1 cinvoke strcat,strResult,strPart2 cinvoke printf,strFormat,strResult invoke ExitProcess,0 section '.idata' import data readable writeable library kernel32,'kernel32.dll',\ msvcrt,'msvcrt.dll' import kernel32,\ ExitProcess,'ExitProcess' import msvcrt,\ printf,'printf',\ strcpy,'strcpy',\ strcat,'strcat' |
|||
22 Aug 2018, 16:54 |
|
DimonSoft 22 Aug 2018, 18:04
C runtime functions should be supplied with obligatory warning about possible security vulnerabilities (buffer overflow) caused by insufficient buffer sizes (in case source string lengths are not known in advance) which has been the problem for C/C++ programs for a few decades now. Not sure if msvcrt.dll provides safe versions.
P.S. The functions still don’t let one put an arbitrary string into 4-byte register. |
|||
22 Aug 2018, 18:04 |
|
NEASM 24 Aug 2018, 13:49
DimonSoft wrote:
Excuse me, i'm not so expert. |
|||
24 Aug 2018, 13:49 |
|
DimonSoft 24 Aug 2018, 16:37
NEASM wrote:
It’s not about expertise, it’s about proper task formulation and common sense. |
|||
24 Aug 2018, 16:37 |
|
ManOfSteel 24 Aug 2018, 19:29
Perhaps NEASM wants eax to hold the pointer to the resulting string, not the actual resulting string?!
|
|||
24 Aug 2018, 19:29 |
|
DimonSoft 24 Aug 2018, 19:53
ManOfSteel wrote: Perhaps NEASM wants eax to hold the pointer to the resulting string, not the actual resulting string?! Which is the most obvious case but that contradicts with NEASM wrote: (ie: ECX = "hel"; EDX = "lo"; EAX = "hello", for example) |
|||
24 Aug 2018, 19:53 |
|
Furs 25 Aug 2018, 15:57
NEASM wrote: Sorry for the trouble, 'hel' is 3 bytes, but 'hello' is 5 bytes so it won't fit. What you ask for is impossible. |
|||
25 Aug 2018, 15:57 |
|
revolution 25 Aug 2018, 16:01
Furs wrote: EAX is 4 bytes. |
|||
25 Aug 2018, 16:01 |
|
DimonSoft 25 Aug 2018, 16:52
revolution wrote:
Or we could use Huffman encoding with custom frequency table. 14 bits ought to be enough for greeting (and even zero-termination). |
|||
25 Aug 2018, 16:52 |
|
revolution 25 Aug 2018, 17:01
At least two ways to encode in 14 bits:
Code: l = 00 e = 01 h = 10 o = 110 _ = 111 ------- 14 bits Code: l = 0 e = 100 h = 101 o = 110 _ = 111 ------- 14 bits |
|||
25 Aug 2018, 17:01 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.