flat assembler
Message board for the users of flat assembler.

Index > Main > Concatenate a string, and return it in EAX

Author
Thread Post new topic Reply to topic
NEASM



Joined: 13 Apr 2018
Posts: 13
NEASM 22 Aug 2018, 01:38
Sorry for the trouble,

I've been looking for a long way to concatenate two strings and return their value to EAX (ie: ECX = "hel"; EDX = "lo"; EAX = "hello", for example), but I've never been able to find nothing about this. Could you give me a function in assembly that concatenates two strings? Thanks in advance.
Post 22 Aug 2018, 01:38
View user's profile Send private message Reply with quote
NEASM



Joined: 13 Apr 2018
Posts: 13
NEASM 22 Aug 2018, 01:39
(i haven't found a good manner to do this in C as well)
Post 22 Aug 2018, 01:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
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'    
Post 22 Aug 2018, 04:22
View user's profile Send private message Visit poster's website Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 430
Location: Australia
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    


Smile

_________________
2 Ton Digital - https://2ton.com.au/
Post 22 Aug 2018, 10:00
View user's profile Send private message Reply with quote
NEASM



Joined: 13 Apr 2018
Posts: 13
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:
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'    


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

    
Post 22 Aug 2018, 10:42
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 22 Aug 2018, 12:47
NEASM wrote:
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

    

First describe how would you store the resulting string in 4 bytes.
Post 22 Aug 2018, 12:47
View user's profile Send private message Visit poster's website Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 155
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' 
    
[/code]
Post 22 Aug 2018, 16:54
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
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.
Post 22 Aug 2018, 18:04
View user's profile Send private message Visit poster's website Reply with quote
NEASM



Joined: 13 Apr 2018
Posts: 13
NEASM 24 Aug 2018, 13:49
DimonSoft wrote:
NEASM wrote:
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

    

First describe how would you store the resulting string in 4 bytes.


Excuse me, i'm not so expert.
Post 24 Aug 2018, 13:49
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 24 Aug 2018, 16:37
NEASM wrote:
DimonSoft wrote:
First describe how would you store the resulting string in 4 bytes.


Excuse me, i'm not so expert.

It’s not about expertise, it’s about proper task formulation and common sense.
Post 24 Aug 2018, 16:37
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 24 Aug 2018, 19:29
Perhaps NEASM wants eax to hold the pointer to the resulting string, not the actual resulting string?!
Post 24 Aug 2018, 19:29
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
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)
and code given later.
Post 24 Aug 2018, 19:53
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2493
Furs 25 Aug 2018, 15:57
NEASM wrote:
Sorry for the trouble,

I've been looking for a long way to concatenate two strings and return their value to EAX (ie: ECX = "hel"; EDX = "lo"; EAX = "hello", for example), but I've never been able to find nothing about this. Could you give me a function in assembly that concatenates two strings? Thanks in advance.
EAX is 4 bytes.

'hel' is 3 bytes, but 'hello' is 5 bytes so it won't fit.

What you ask for is impossible.
Post 25 Aug 2018, 15:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 25 Aug 2018, 16:01
Furs wrote:
EAX is 4 bytes.

'hel' is 3 bytes, but 'hello' is 5 bytes so it won't fit.
We could try to use a different character set. A character set with 6 bits per character would allow 5 characters within 30 bits.
Post 25 Aug 2018, 16:01
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 25 Aug 2018, 16:52
revolution wrote:
Furs wrote:
EAX is 4 bytes.

'hel' is 3 bytes, but 'hello' is 5 bytes so it won't fit.
We could try to use a different character set. A character set with 6 bits per character would allow 5 characters within 30 bits.

Or we could use Huffman encoding with custom frequency table. 14 bits ought to be enough for greeting (and even zero-termination).
Post 25 Aug 2018, 16:52
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
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    
Post 25 Aug 2018, 17:01
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.