Author Topic: Need help with assembler-code  (Read 3422 times)

0 Members and 1 Guest are viewing this topic.

Jigo

  • Guest
Need help with assembler-code
« on: 2005-12-31 08:35:41 »
Hi,

I want to write a kind of basic-compiler, which compiles a basic language into assembler-code (MASM). I've already finished the scanner and the parser, but now I have problems with creating the asm-code. I must admit that I have no great knowledge about assembler and so I need some help. In the moment my greatest problem is to concatenate two strings. In the MASM-Package I found the szMultiCat and the szCopy function, which help my with this problem.
Code: [Select]
.Data
stext1 DB "Hallo ",0
stext2 DB "World!",0

.Data?
StringBuffer DB 100 DUP(?)
.Code

push Offset stext2
push Offset stext1
push Offset StringBuffer
push 2
call szMultiCat
push Offset stext1
push Offset StringBuffer
call szCopy

But if the string is to small to hold the new content, other strings will be overwritten. I hope you can help my.
« Last Edit: 2006-01-04 10:40:23 by Jigo »

gerard

  • Guest
Re: Need help with assembler-code
« Reply #1 on: 2005-12-31 09:44:29 »
In MASM if really it is in MASM you want to write code..

thre concatenation of t\wo strings may be done with API lstrcat.....

Quote
.Data
stext1        DB "Hallo ",0
stext2       DB "World!",0
Blank          DB  32,32,0


.Data?
StringBuffer DB 100 DUP(?)

.Code

invoke lstrcpy,ADDR StringBuffer,ADDR stext1
invoke lstrcat,ADDR StringBuffer,ADDR Blank
invoke lstrcat,ADDR StringBuffer,ADDR stext2
 
 


I think my answer will be the welcomer...

----
Gerard

Offline roticv

  • Community Staff
  • ASM Fanatic
  • *****
  • Posts: 2175
    • http://roticv.rantx.com
Re: Need help with assembler-code
« Reply #2 on: 2005-12-31 10:19:02 »
Gerand, his code is perfect (some people just dont like using invoke)

Personally I would suggest that you use dynamically allocated strings if you want string size that can expand and contract (That's the purpose of these dyanmically allocated data right?). However, I would suggest you start simple and get use to MASM first. Enjoy your project.

Regards,
Victor
Discussions based purely on opninions generally lead to flame wars, discussions based on experience can actually enlighten. But those based on a combination simply go nowhere. - E?in

Jigo

  • Guest
Re: Need help with assembler-code
« Reply #3 on: 2005-12-31 12:25:25 »
Thank you for your code Gerard, but it doesn't solve my problem. If I copy the content of StringBuffer back into stext1 then other strings will be overwritten.

In MASM if really it is in MASM you want to write code..

Yes, I use MASM, but I don't use invoke, because it's easier to pass the parameters of the token-stream created by my scanner by the stack than by invoke.

Personally I would suggest that you use dynamically allocated strings if you want string size that can expand and contract

Yes, I think that's the way I must take, but can you give my a advice how? If I use following code I can expand and contract the size of the strings stext1 and stext2, but I also must declare the static strings _stext1 and _stext2. Is there an other possibility without defining _stext1 and _stext2?

Code: [Select]
.Data
_stext1 DB "Hallo",0
_stext2 DB " World!",0
.Data?
StringBuffer DB 100 DUP(?)

stext1 DB 100 DUP(?)
stext2 DB 100 DUP(?)

.Code
push Offset _stext1
push Offset stext1
call lstrcpy

push Offset _stext2
push Offset stext2
call lstrcpy




regards,
Jigo
« Last Edit: 2005-12-31 18:31:48 by Jigo »

Offline Raymond

  • Regular Member
  • ***
  • Posts: 620
    • RAYMOND'S PAGE
Re: Need help with assembler-code
« Reply #4 on: 2006-01-01 00:24:45 »
Quote
Is there an other possibility without defining _stext1 and _stext2?

Simply reserve yourself work buffers in the .DATA section, the size of which will be large enough for the largest string sizes you could expect. Rezero the first byte of a work buffer whenever you want to start concatinating new strings into it.
Whenever you assume something, you risk being wrong half of the time.

Offline roticv

  • Community Staff
  • ASM Fanatic
  • *****
  • Posts: 2175
    • http://roticv.rantx.com
Re: Need help with assembler-code
« Reply #5 on: 2006-01-01 02:04:50 »
Instead of defining the string as what you did, it is better you call HeapAlloc then you store the pointer or something.
Discussions based purely on opninions generally lead to flame wars, discussions based on experience can actually enlighten. But those based on a combination simply go nowhere. - E?in

Offline f0dder

  • Community Staff
  • ASM Fanatic
  • *****
  • Posts: 7788
  • Front Line Assembly
    • http://f0dder.reteam.org
Re: Need help with assembler-code
« Reply #6 on: 2006-01-01 20:55:23 »
Quote
Is there an other possibility without defining _stext1 and _stext2?

Simply reserve yourself work buffers in the .DATA section, the size of which will be large enough for the largest string sizes you could expect. Rezero the first byte of a work buffer whenever you want to start concatinating new strings into it.


This would be a pretty bad way to do it, since the code wouldn't be re-entrant that way.

Have a look at http://www.asmcommunity.net/board/index.php?topic=21665.0 for some dynamic string handling code... It needs to be updated a bit, I never got around to it... might do in the near future, though :)
- carpe noctem

Jigo

  • Guest
Re: Need help with assembler-code
« Reply #7 on: 2006-01-02 10:14:53 »
Thank you f0dder your code is great.