Arrays 汇编NASM x86-将字符放入数组
我需要制作一个程序,打开一个文件,逐字符读取,并将其保存到汇编NASM x86中的数组中。目前,程序能够使用stdin打开文件,并使用getchar()读取字符。但是,我一直在将字符保存到数组中,需要这方面的帮助 多谢各位Arrays 汇编NASM x86-将字符放入数组,arrays,assembly,x86,nasm,Arrays,Assembly,X86,Nasm,我需要制作一个程序,打开一个文件,逐字符读取,并将其保存到汇编NASM x86中的数组中。目前,程序能够使用stdin打开文件,并使用getchar()读取字符。但是,我一直在将字符保存到数组中,需要这方面的帮助 多谢各位 ; Run it this way: ; test < (input file) ; Build using these commands: ; nasm -f elf -g -F stabs test.asm ; gcc –m32 -o test.o test ;
; Run it this way:
; test < (input file)
; Build using these commands:
; nasm -f elf -g -F stabs test.asm
; gcc –m32 -o test.o test
;
SECTION .bss ; Section containing uninitialized data
TextLenght EQU 1024 ; Define length of a line of text data
Text resb TextLenght ; Define array
SECTION .data ; Section containing initialised data
fileFmt: db "%c",0
SECTION .text ; Section containing code
extern putchar
extern getchar
extern printf
global main; Linker needs this to find the entry point!
main :
start:
nop ; This no-op keeps gdb happy...
mov eax, 0
mov edx, 0
mov ecx, 0
mov ebx, 0
; Read a buffer full of text from stdin:
read:
call getchar ; call getchar to get input from stdin, char is save in eax
cmp al, -1
jle Done ; if return -1, we at EOF
cmp eax, 97 ; get rid of 'enter' char
jl read
;mov Text, eax; try to save char in eax into array, don;t work
push eax ;push eax to print
push fileFmt
call printf
add esp, 8 ; clear the stack
jmp read
Done:
mov eax,1 ; Code for Exit Syscall
mov ebx,0 ; Return a code of zero
int 80H ; Make sys_exit kernel call
;按以下方式运行:
; 测试<(输入文件)
; 使用以下命令生成:
; nasm-f elf-g-f stabs test.asm
; gcc–m32–o测试。o测试
;
第三节bss;包含未初始化数据的节
文本长度等于1024;定义文本数据行的长度
Text resb Text length;定义数组
第二节数据;包含初始化数据的节
fileFmt:db“%c”,0
第节.案文;包含代码的节
普查尔外景酒店
外部getchar
外部打印
全球干线;链接器需要这个来找到入口点!
主要内容:
开始:
否;这个“不行动”让gdb很开心。。。
mov-eax,0
mov-edx,0
mov-ecx,0
mov-ebx,0
; 从stdin读取一个充满文本的缓冲区:
阅读:
调用getchar;调用getchar从stdin获取输入,char保存在eax中
cmp铝-1
jle完成;如果返回-1,我们在EOF
cmp-eax,97;去掉“enter”字符
jl-read
;mov文本,eax;尝试将eax中的字符保存到数组中,don;行不通
推动eax;按eax打印
推送文件
调用printf
添加esp,8;清除堆栈
jmp读取
完成:
mov-eax,1;退出系统调用的代码
mov-ebx,0;返回零的代码
int 80H;使系统退出内核调用
嗯,您是否尝试过mov[Text],al
?要检查getchar()
中的EOF/错误,请检查所有eax
。否则,您无法区分EOF和读取0xFF
字节(例如作为UTF-8字符串的一部分)之间的区别。这就是为什么在调用getchar
之前将eax、ecx和edx归零是无用的。您必须假设对未自己编写的函数的每个函数调用都会修改这些寄存器。您使用一个exit
系统调用而不是返回,因此您可以关闭调用方的ebx
@Michael:所以我做了上面建议的更改,现在就编译代码,但是如何以文本形式打印内容呢。我试图用“push Text”替换“push eax”,但它不会打印任何内容。