Hello There, Guest!
View New Posts  |  View Today's Posts
NASM (Assembly) - Finding Null-terminated string length

  • 0 Vote(s) - 0 Average


04-25-2015, 06:56 PM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

NASM (Assembly) - Finding Null-terminated string length
Here's a quick implementation I wrote using nasm for finding the length of a local null terminated string, without calling the C function strlen(). The only C function used in this example is printf() to display the results to the console.

Code:
;----------------------------------------------------
; File:
;   code.asm
;
; Description:
;   NASM implementation to find the length of a
;   local null-terminated string.
;
; nasm -f win32 code.asm -o code.o
; gcc -o main code.o
;----------------------------------------------------

extern _printf ; C function

global _main

section .data
  mystr: db 'testing', 0
  format: db 'length (%s) => %u', 10, 0

section .text
_main:
  push dword mystr
  call strlen
  add esp, byte 4
  push eax
  push dword mystr
  push dword format
  call _printf
  add esp, byte 12
  ret

strlen:
  mov ebx, [esp + 4]
  mov ecx, 0
  .next:
  cmp byte [ebx], 0
  jz .exit_loop
  inc ebx
  inc ecx
  jmp .next
  .exit_loop:
  mov eax, ecx
  ret
This post was last modified: 04-25-2015, 08:13 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   High Compression Demo - Assembly 2008 AceInfinity 1 1,267 08-25-2013, 12:10 PM
Last Post: AceInfinity
   Convert ACSII to String Values [New GUI Project] AceInfinity 0 1,732 11-30-2011, 01:32 AM
Last Post: AceInfinity
   Multi ACSII <> String Converter Script AceInfinity 1 1,972 11-09-2011, 08:01 PM
Last Post: AceInfinity
   String Hasher AceInfinity 1 1,251 10-12-2011, 05:50 PM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)