Assembly 关于字符串长度计数的几个问题
我对计算字符串的长度有问题。 我总是得到一些像2432这样的数字,我传递一个像“abc”这样的字符串 我认为问题出在这方面Assembly 关于字符串长度计数的几个问题,assembly,nasm,Assembly,Nasm,我对计算字符串的长度有问题。 我总是得到一些像2432这样的数字,我传递一个像“abc”这样的字符串 我认为问题出在这方面 mov bl, byte [esi] 但我不知道为什么。 也许它是以位为单位的字符长度 问题是64位操作系统还是双核处理器?(我有点怀疑,因为我认为第一行“bits32”应该可以解决这个问题) 注:这是一个练习,这就是为什么我需要像这样确定字符串的长度 守则: bits 32 extern _printf extern _scanf global _main secti
mov bl, byte [esi]
但我不知道为什么。
也许它是以位为单位的字符长度
问题是64位操作系统还是双核处理器?(我有点怀疑,因为我认为第一行“bits32”应该可以解决这个问题)
注:这是一个练习,这就是为什么我需要像这样确定字符串的长度
守则:
bits 32
extern _printf
extern _scanf
global _main
section .data
number_frmt db 10,"%d",10,0
enter_str db "Enter some string: ", 10,0
string_frmt db "%s", 0
section .bss
entered_string resb 100
section .text
_main:
pushad
push dword enter_str
call _printf
add esp, 4
push dword entered_string
push dword string_frmt
call _scanf
add esp, 4 ;leave the entered string in the stack
call count ; count it and put the result to eax
push dword eax
push dword number_frmt
call _printf
add esp, 12
popad
ret
count:
push esi ;save it
push ebx ;save it
mov eax, 0 ;init eax=0
mov esi, [esp+12] ;put the entered string to esi
.loop:
mov bl, byte [esi] ;get the first char
inc eax ;eax++
add esi,1 ;point to next char
cmp bl,10 ;is it new line?
jne .loop ;if not loop
dec eax ;eax-- (because of newline at the end)
pop ebx ;retrieve ebx
pop esi ;retrieve esi
ret
应该是
cmp bl,0
因为c/c++字符串每次都以0结尾/终止,所以实际上您已经在内存中搜索了下一个10所在的随机位置
应该是
cmp bl,0
因为c/c++字符串每次都以0结尾/终止,所以实际上您已经在内存中搜索了下一个10所在的随机位置。使用scanf可能不是一件好事,因为它会混淆问题,使用GET切换,新行也是有效字符,并且应该作为计数的一部分进行计数。字符串以NUL结尾(自动)
使用scanf可能不是一件好事,因为它会混淆问题,使用gets切换,新行也是有效字符,应该作为计数的一部分进行计数。字符串以NUL结尾(自动)
count:
push esi ;save it
push ebx ;save it
xor eax, eax; initialize it to zero
mov esi, [esp+12] ;put the entered string to esi
.loop:
mov bl, byte [esi] ;get the first char
cmp bl, bl ;set the flags
jz .out ;nul character
inc eax
jmp .loop
pop ebx ;retrieve ebx
pop esi ;retrieve esi
ret