Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux IA32汇编语言从文件中读取整数_Linux_Assembly_X86 - Fatal编程技术网

Linux IA32汇编语言从文件中读取整数

Linux IA32汇编语言从文件中读取整数,linux,assembly,x86,Linux,Assembly,X86,我试图从一个文件中读取整数,然后将它们输出到一个新文件中。我想我最困惑的地方是读整数的时候。请看一下我的代码,顺便说一下,我只是汇编语言的初学者 .data filename: .asciz "indata.txt" out: .asciz "outdata.txt" .bss .lcomm num, 5 .lcomm filehandle, 4 .text .globl _start _start: # Open input file movl $5, %eax #open file mov

我试图从一个文件中读取整数,然后将它们输出到一个新文件中。我想我最困惑的地方是读整数的时候。请看一下我的代码,顺便说一下,我只是汇编语言的初学者

.data
filename:
.asciz "indata.txt"
out:
.asciz "outdata.txt"
.bss
.lcomm num, 5
.lcomm filehandle, 4
.text
.globl _start
_start:

# Open input file
movl $5, %eax #open file
movl $filename, %ebx #filename
movl $00, %ecx #rd
movl $0444, %edx
int $0x80
test %eax, %eax #Test
js badfile
movl %eax, filehandle

# Open output file
movl $5, %eax
movl $out, %ebx
movl $01101, %ecx
movl $0644, %edx
int $0x80
test %eax, %eax
js badfile
movl %eax, out

readloop:
#Read and store input.
movl $3, %eax #Read
movl filehandle, %ebx   
movl $num, %ecx #memory adress of input string
movl $5, %edx #length
int $0x80       
test %eax, %eax #Test
jz done
js badfile
movl %eax, %edx

#Convert to number
subl $48, num


# Write output to file
movl $4, %eax
movl out, %ebx
movl $num, %ecx
int $0x80
test %eax,%eax
js badfile
jmp readloop

badfile:
movl %eax, %ebx
movl $1, %eax
int $0x80

done:

#Close file.
movl $6, %eax
movl filehandle, %ebx
int $0x80

#Close file.
movl $6, %eax
movl out, %ebx
int $0x80

movl $1, %eax
movl $0, %ebx
int $0x80

它在哪里失败,您的文件包含什么?我只是想确保文件包含原始二进制数据,而不是ASCII文本。输出包括一个包含数字的框。我假设这是原始数据,也包括一些数字,甚至是一个“\0”。有没有办法逐行读取,忽略新行字符,而不是指定的字节数?我相信导致错误的原因是,如果我读取“1”,它将读取它以及新行的字符和下一行的字节。对我来说,这听起来像ASCII码——你的代码甚至会这么说,这就是它从输入中减去48得到实际数字的原因。我怀疑您在文件中遇到的值不是有效的ASCII数字google ASCII表,并且它提前退出。您需要将已读取的字符与任何特殊值(如\0,实际上为零)进行比较,如果匹配,则跳回readloop的开头。如果有疑问,请使用十六进制编辑器查看文件内容的真实外观。仅仅依靠记事本真的会把你绊倒。