0

I noticed at my ASM book that author uses that LENGTHOF str for finding the size of byte it is searching. Code:

.data
alpha BYTE "ABCDEFGH",0
.code
mov edi,OFFSET alpha            ; EDI points to the string
mov al,'F'                      ; search for the letter F
mov ecx,LENGTHOF alpha          ; set the search count
cld                             ; direction = forward
repne scasb                     ; repeat while not equal
jnz quit                        ; quit if letter not found
dec edi                         ; found: back up EDI

Is there a way to replace the LENGTHOF alpha part? I am aware that len equ $-alpha exists but for the sake of learning i search for more under the hood and "primitive" practices. So that I can understand better how that really works.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
Ali A
  • 53
  • 6
  • The `repne scasb` should not try to find content **where the zero-terminator sits**. Therefore `mov ecx, LENGTHOF alpha - 1` is already better! – Sep Roland Jan 07 '21 at 01:15

2 Answers2

2

You are right, LENGTHOF alpha could be replaced with len equ $-alpha declared at the very tail of alpha definition:

.data
alpha BYTE "ABCDEFGH",0
len equ $-alpha
.code
 mov ecx,len

Operators LENGTHOF and OFFSET can be treated as names of functions which return the number of bytes, in your example 9 and 0. They are calculated at assembly time and their names depend on the assembler. In MASM they are called operator LENGTHOF and operator OFFSET, in other assemblers the name may differ, for instance see attributes in €ASM.

vitsoft
  • 2,586
  • 1
  • 12
  • 25
  • They're not "functions" in the asm sense of something you can `call`. A better term might be "operators". Related: [How does $ work in NASM, exactly?](https://stackoverflow.com/q/47494744) for details of what `$-alpha` is really doing. – Peter Cordes Jan 06 '21 at 07:37
2

If it's not an assemble-time constant string, then you'd either

  • know it's length already (because of how it was input) so you could still use rep scasb as a slow-ish memchr, with that runtime variable length as ECX.

  • or if it's an "implicit length" C-style string and you just have a pointer, you could search for the terminating 0 or 'F' (i.e. implement strchr) using a loop that checks for both things.

Or the even slower way would be to search for the 0 first to find the length (i.e. implement strlen), then use that as ECX for your rep scasb (memchr).

Obviously all these search loops can be done with simple instructions in a manual loop like mov and cmp/jne; rep scasb isn't magic.

strchr and strlen and so on are names of standard C functions. I'm using them as shorthand to describe the search loop you'd want to use.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606