flat assembler
Message board for the users of flat assembler.

Index > Windows > how to cute a buff?

Author
Thread Post new topic Reply to topic
crossbr



Joined: 11 Dec 2006
Posts: 2
crossbr
hi, i am a new user of fasm, i developing a client/server program, and i need cute a buff in few commands, (parameters)
in obj. pascal this code

copy(var, index, length): string;

in asm, this functions ins't avaliable, what a command to make this operation?

sorry my badly english Wink

thanks.
Post 11 Dec 2006, 21:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Well actually at run-time the formal parameters look like this

procedure copy(var:string; index, length:integer; var result: string);

This conversion from function to procedure is required because a string obviously doesn't fit in a CPU register (the same happens when you don't return simple types).

Well after the conversion this is one way

Code:
proc copy, var, index, length, result
  push    esi edi

  mov     esi, [var]
  mov     edi, [result]
  add     esi, [index]
  mov     ecx, [length]
  rep     movsb

  pop     edi esi
  ret
endp    


Please note that the proc I written is unsafe since it doesn't checks if index is beyond the last character nor checks if length is too large. So assuming that the string are ASCIIZ (null terminated like in C) here another proc

Code:
proc copy, var, index, length, result
  push    esi edi

  mov     esi, [var]
  mov     edi, [result]
  mov     ecx, [index]
  test    ecx, ecx
  jz      .done

.checkZero: 
  lodsb
  test    al, al
  jz      .exit
  dec     ecx
  jnz     .checkZero

.done: 
  mov     ecx, [length]
  test    ecx, ecx
  jz      .exit

.copyChars:
  lodsb
  stosb
  test    al, al
  jz      .return
  dec     ecx
  jnz     .copyChars

.exit: 
  mov     byte[edi], 0

.return: 
  pop     edi esi
  ret
endp    


[edit]Actually the Pascal-like procedure declaration of my ASM codes is:
procedure copy(const var: string; index, length: integer; var result: string); {string type actually is ASCIIZ in my code}[/edit]
Post 11 Dec 2006, 22:35
View user's profile Send private message Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
Almost none of the mem/string instructions in C/C++ are safe, don't feel bad Smile. Damn NULL pointers.

_________________
redghost.ca
Post 11 Dec 2006, 23:15
View user's profile Send private message AIM Address MSN Messenger Reply with quote
crossbr



Joined: 11 Dec 2006
Posts: 2
crossbr
thanks to help! =P
Post 12 Dec 2006, 01:12
View user's profile Send private message 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.