AceInfinity
Emeritus, Contributor
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.
Written for 32-bit Windows.
Code:
[NO-PARSE];----------------------------------------------------
; 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[/NO-PARSE]
Written for 32-bit Windows.