我正在用x64 asm编写一些小程序,但我在完成这个程序时遇到了麻烦。
我在scasb指令上遇到了段错误。我猜这个问题来自strLen块中的mov eax, my_str ; address of buffer passed to ... ?
行。我试过不同的寄存器,当然eax不是正确的(32位)使用。我在FreeBSD上,因此是系统调用。
有什么想法吗?
以下是代码:
;
; StrLen.asm : Read a string on stdin and return its length using a strLen replacement
;
section .data
userMsg :
db "Please enter a string (max 1024 bytes): ", 0
lenUserMsg equ $-userMsg
dispMsg :
db "Length: ", 0
lenDispMsg equ $-dispMsg
section .bss ;Uninitialized data
my_strLength equ 1024 ; Input buffer size
my_str :
resb my_strLength ; Input buffer
section .text
global _start
_start:
;;; User Prompt ;;;
mov rax, 4 ; write
mov rdi, 1 ; stdout
mov rsi, userMsg ; msg text
mov rdx, lenUserMsg ; msg length
syscall
;;; Read and store the string ;;;
mov rax, 3 ; read
mov rdi, 0 ; stdin
mov rsi, my_str ; address of my_str passed to rsi
mov edx, my_strLength ; read n bytes in one go
syscall
;mov r9, rax ; keep sys_read return value (nb of bytes read)
;;; strLen() ;;;
mov eax, my_str ; address of buffer passed to ... ?
sub al, al ; zeroes al
not rcx ; 1/ecx = ecx highest value
cld ; clear direction flag for byte search
repne scasb ; repeat byte search until match
not rcx ; ecx contains 1/length +1
dec rcx ; -1 & ok we have the length
;;; Write back the length ;;;
mov rax, rdi ; write
mov rdi, 1 ; stdout
; mov rcx, rcx ; nb bytes read
mov rdx, my_strLength
syscall
;;; Exit ;;;
mov rax, 1 ; exit
xor rdi, rdi ; return value
syscall