As this will eventually become part of an operating system I'm developing, there is no specific adherence to any particular ABI
, even though this is being developed and tested on Ubuntu 16.04. My OS will however adopt a SYSTEM V paradigm insomuch as passing values by register, but they will be passed in the one most suitable to the algorithm.
One of my methodologies is to utilize as many bits of the register as possible, especially when 16 or less are significant. I have found too, that in a lot of cases this can simplify calling process. It does however complicate things a little ie:
mov rdi, Pointer_to_String
mov ecx, 784 << 8 | '.'
call strlen
This would traverse the buffer pointed to by RDI for the first occurence of period for a maximum of 784 characters. This snippet had evolved from just scanning for NULL for a maximum of FFFFFFFFH bytes, to scanning for any of the values (0 - FF), to traversing buffer for specified number of bytes.
Question
Do you think I've achived the maximum utility with the least amount of code.
; Determine length, including terminating character EOS. Result may include
; VT100 escape sequences.
; ENTER: RDI = Pointer to ASCII string.
; RCX Bits 31 - 08 = Max chars to scan (1 - 1.67e7)
; 07 - 00 = Terminating character (0 - FF)
; LEAVE: RAX = Pointer to next string (optional).
; FLAGS: ZF = Terminating character found, NZ otherwise (overrun).
; DF = Unmodified incase it was already set.
strlen: pushf ; Preserve DF (Direction flag)
push rcx ; Preserve registers used by proc so
push rdi ; its non-destructive except for RAX.
mov al, cl ; Byte to scan for in AL.
shr ecx, 8 ; Shift max count into bits 23 - 00
std ; Auto decrement.
repnz scasb ; Scan for AL or until ECX = 0
mov rax, rdi ; Return pointer to EOS + 1
pop rdi ; Original pointer from proglogue
jz .exit ; ZF indicates EOS was found
mov rax, rdi ; RAX = RDI, NULL string
.exit:
pop rcx
popf ; Restore direction flag.
ret