How would I find the length of a string using NASM?

nasm string length
find character in string - assembly
length of string in assembly language
find string length assembly x86
how to use nasm
nasm program
nasm assembly
nasm syntax

I'm trying to make a program using NASM that takes input from command line arguments. Since string length is not provided, I'm trying to make a function to compute my own. Here is my attempt, which takes a pointer to a string in the ebx register, and returns the length of the string in ecx:

len:
    push ebx
    mov ecx,0
    dec ebx
    count:
        inc ecx
        inc ebx
        cmp ebx,0
        jnz count
    dec ecx
    pop ebx
    ret

My method is to go through the string, character by character, and check if it's null. If it's not, I increment ecx and go to the next character. I believe the problem is that cmp ebx,0 is incorrect for what I'm trying to do. How would I properly go about checking whether the character is null? Also, are there other things that I could be doing better?

You are comparing the value in ebx with 0 which is not what you want. The value in ebx is the address of a character in memory so it should be dereferenced like this:

cmp byte[ebx], 0

Also, the last push ebx should be pop ebx.

String Length Function in NASM, In certain situations when I want to print some string to stdout we need the length for the write syscall in linux. So we can't always depend on the  The single-line macro __NASM_VER__ expands to a string which defines the version number of nasm being used. So, under NASM 0.98.32 for example, db __NASM_VER__ would expand to. db "0.98.32" 4.11.4 __FILE__ and __LINE__: File Name and Line Number. Like the C preprocessor, NASM allows the user to find out the file name and line number containing the current instruction.

strlen() implementation in NASM · Cogs and Levers, I see people using the $ - msg macro to calculate the length of their string at assemble time. In today's post, I'll show you how to measure the  I'm trying to make a program using NASM that takes input from command line arguments. Since string length is not provided, I'm trying to make a function to compute my own. Here is my attempt, which takes a pointer to a string in the ebx register, and returns the length of the string in ecx:

Here how I would have coded it

len:
      push ebx
      mov  eax, ebx
lp:
        cmp byte [eax], 0
        jz  lpend
        inc eax
        jmp lp
lpend:
        sub eax, ebx

      pop ebx
      ret

(The result is in eax). Likely there are better ways.

String length calculation implementation in Assembly (NASM , I have a number of comments that I hope you find helpful. Code comments. First, your code is generally well commented and I had little difficulty  NASM has the capacity to define other special symbols beginning with a double period: for example, ..start is used to specify the entry point in the obj output format (see section 8.4.6), ..imagebase is used to find out the offset from a base address of the current image in the win64 output format (see section 8.6.1). So just keep in mind that

Chapter 11 - X86-SSE Programming - Text Strings, 1 ;jump if '\0' byte not found ; Found '\0' byte in current block (index in ECX) ; Calculate string length and return add eax,ecx ;eax = ptr to '\0' byte sub eax,s ;eax​  NASM recommends you use one of the following web browsers and always make sure it is up to date; Latest version of Chrome , Safari 10+, Latest version of Firefox, Edge. NASM also recommends using the following Operating systems for the best experience: Windows 7+, Mac 10.10+, iOS 11+, Android 5+.

Manual String Length function in NASM · GitHub, Manual String Length function in NASM. strlen.asm. %if 0. * Title: Manual String Length function. * Author: Osanda Malith (@OsandaMalith). * Website:  To find the length of a string, we need to initialize ECXto the highest value possible, which is 4,294,967,295in the 32-bit mode. When viewed as a signed value, it is the same as -1. The easiest way to achieve that is by first setting ECXto 0, then reversing all its bits, e.g.: sub ecx, ecx ; ECX = 0

Chapter 4: The NASM Preprocessor, The use of commas to separate strings is permitted but optional. 4.2.2 String Length: %strlen. The %strlen operator assigns the length of a string to a macro. For  strlen() implementation in NASM 08 Jan 2013 Introduction. Seeing so many “Hello, world” concepts for getting up an running in Assembly has annoyed me a little bit. I see people using the $ - msg macro to calculate the length of their string at assemble time.

Comments
  • cmp ebx,0 is wrong and also the push ebx at the end should probably be pop ebx (otherwise you'll get a stack overflow !).
  • Oops. I mistakenly hand-copied pop ebx as push ebx.
  • try and get into the habit of copying and pasting your actual code, rather than re-typing it. Also please edit your question so that it matches the actual code.
  • @paul-r That's what I usually do. I was working in VirtualBox and clipboard sharing was not set up properly.
  • Pointer-increment is better, but the terrible loop structure is a bottleneck on many CPUs. You have 2 branches (one taken, one not-taken) in each loop iteration, so it can only run at one iteration per 2 clocks on CPUs before Haswell, i.e. ones that existed when you wrote this. Why are loops always compiled into "do...while" style (tail jump)?. (Of course only checking one byte at a time is pretty terrible for modern x86 with SSE2.)