flat assembler
Message board for the users of flat assembler.

Index > Main > strncpy?

Author
Thread Post new topic Reply to topic
thecf



Joined: 23 Dec 2006
Posts: 23
thecf 05 Jan 2007, 18:01
esi = 'somefile.bin'

how can i remove the last four bytes from esi so it reads 'somefile'?
Post 05 Jan 2007, 18:01
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 05 Jan 2007, 19:01
Code:
proc strncpy dest, src, maxLength
  push    esi edi 

  mov     edi, [dest] 
  mov     esi, [src] 
  mov     ecx, [maxLength]
  test    ecx, ecx
  jz      .exit

.storeChar: 
  lodsb 
  stosb 
  test    al, al 
  jz      .exit 

.checkLength: 
  sub     ecx, 1 
  jnc     .storeChar 

  mov     byte [edi-1], 0

.exit: 
  pop     edi esi 
  ret 
endp    


That code is for strncpy, now if you just want to copy up to the file extention this could be a way:
Code:
proc removeExt buffer, fileName, bufferLength
  push    esi edi ebx

  mov     edi, [buffer]
  mov     esi, [fileName]
  mov     ecx, [bufferLength]
  mov     ebx, -1
  test    ecx, ecx
  jz      .return

.storeChar:
  lodsb
  stosb
  test    al, al
  jz      .exit
  cmp     al, '.'
  jne     .checkLength

  mov     ebx, esi

.checkLength:
  sub     ecx, 1
  jnc     .storeChar

  mov    byte [edi-1], 0
.exit:
  cmp    esi, ebx
  jb     .return

  mov    byte [ebx-1], 0

.return:
  pop     ebx edi esi
  ret
endp    


(Both codes untested, sorry, no time to check them)

[edit]I corrected the codes to ensure that the destination string will be null terminated (if the length is above 0)[/edit]
Post 05 Jan 2007, 19:01
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 06 Jan 2007, 07:44
Code:
mov byte [esi+8], 0
    

Razz
Post 06 Jan 2007, 07:44
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 06 Jan 2007, 18:00
Heh, f0dder, you read our minds. Laughing
Post 06 Jan 2007, 18:00
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 07 Jan 2007, 15:03
...of course what the original poster probably wants is a routine to split a path into {path}{filename}{extension} components, but hey - Smile
Post 07 Jan 2007, 15:03
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 07 Jan 2007, 15:12
The original poster keeps quiet so we still don't know if f0dder solution (which I was very tempted to post too Razz) or my solution fits his needs.
Post 07 Jan 2007, 15: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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.