如何计算汇编中字符串中的字符和行?
How to count character and line from a string in assembly?
我要写UNiX/LINUX命令对应的汇编代码wc
此代码仅供测试。
global main
extern printf
section .data
fmt:
db "%ld %ld",10,0
msg:
db 'CSE DU',10,'Dhaka',10,'Bangladesh',0
;db 'CSE DU',10,0
section .text
main:
push rbp
mov rcx , 0 ; rcx = number of charecter
mov rbx , 0 ; rbx = number of line
lp:
mov rax , [msg+rcx]
inc rcx
cmp rax , 0
je exit ; jump if rax = 0 means EOF ; break
cmp rax , 10
jne lp ; jump if rax != 10 means not new line
inc rbx ; increment rbx if new line found
jmp lp ; continue
exit:
dec rcx
mov rdi , fmt
mov rsi , rbx
mov rdx ,rcx
call printf
pop rbp
ret
此代码的结果是 0 23
,但正确的结果是 2 23
。
如果我使用 msg: db 'CSE DU',10,0
(在代码中被注释掉)那么结果是正确的,结果是 1 7
我在 google 中进行了搜索,但没有为我的平台找到任何解决方案。
我正在使用 Ubuntu(Linux),我的机器是 64 位的,汇编程序是 NASM 并使用 c printf 函数。
我花了很多时间但没有发现问题。如果有人检测到请帮助我。
读取字符时,只有8位宽,所以只读取和比较8位值
mov al, [msg+rcx]
inc rcx
cmp al, 0
je exit
cmp al, 10
jne lp
inc rbx
jmp lp
我要写UNiX/LINUX命令对应的汇编代码wc
此代码仅供测试。
global main
extern printf
section .data
fmt:
db "%ld %ld",10,0
msg:
db 'CSE DU',10,'Dhaka',10,'Bangladesh',0
;db 'CSE DU',10,0
section .text
main:
push rbp
mov rcx , 0 ; rcx = number of charecter
mov rbx , 0 ; rbx = number of line
lp:
mov rax , [msg+rcx]
inc rcx
cmp rax , 0
je exit ; jump if rax = 0 means EOF ; break
cmp rax , 10
jne lp ; jump if rax != 10 means not new line
inc rbx ; increment rbx if new line found
jmp lp ; continue
exit:
dec rcx
mov rdi , fmt
mov rsi , rbx
mov rdx ,rcx
call printf
pop rbp
ret
此代码的结果是 0 23
,但正确的结果是 2 23
。
如果我使用 msg: db 'CSE DU',10,0
(在代码中被注释掉)那么结果是正确的,结果是 1 7
我在 google 中进行了搜索,但没有为我的平台找到任何解决方案。 我正在使用 Ubuntu(Linux),我的机器是 64 位的,汇编程序是 NASM 并使用 c printf 函数。
我花了很多时间但没有发现问题。如果有人检测到请帮助我。
读取字符时,只有8位宽,所以只读取和比较8位值
mov al, [msg+rcx]
inc rcx
cmp al, 0
je exit
cmp al, 10
jne lp
inc rbx
jmp lp