【发布时间】:2019-04-07 02:03:42
【问题描述】:
嗨,我完成了这个令人兴奋的任务,实际上几乎完成了.. 但它在一个有趣的方面失败了。 任务是加载带有整数的文件,对它们进行排序并将它们写入文件。耶...好吧,我的程序正在这样做,但它保留了原始内容。即:
让我们说 45 32
应该得到有序的内容 32 45
好吧,我的程序保留了原始内容并添加了新内容: 45 32 32 45。
那么有什么解决这个问题的建议吗?尽管删除了原始文件并以相同的名称创建了一个新文件。但这有点失败,如果文件包含非整数并且我的代码有错误报告。
这里给出重要的代码:
_OpenFile:
movq $2, %rax # open file
movq $inputfile, %rdi # filename
movq $2, %rsi # read and write
movq $0644, %rdx # setting proper permissions
syscall
ret
还有:
_PrintNumber: #needs rdi as numberholder
movq $1, %r9 # count the number of chars to print
push $10 # store the chars on the stack, we always have '\n'
movq %rdi, %rax # things are easier with it in rax
movq $10, %rcx
decode_loop:
movq $0, %rdx
idivq %rcx # do rdx:rax / rcx
addq $48, %rdx # convert the remainder to an ASCII digit
pushq %rdx # and save it on the stack
addq $1, %r9 # while counting the number of chars
cmpq $0, %rax
jne decode_loop # loop until rax == 0
write_loop:
movq $1, %rax # write
movq $3, %rdi # to the file
movq %rsp, %rsi # which is stored here
movq $1, %rdx # a single character/byte
syscall
addq $8, %rsp # pop the character
addq $-1, %r9 # correct the char count
jne write_loop # loop until r9 reaches 0
ret
感谢所有愿意对此发表评论的人!
【问题讨论】:
-
您好,请编辑问题以包含您的源代码。目前我们根本没有什么可做的。谢谢。
-
当然 :P 虽然它可能更笼统。
-
离题系统编程提示:您应该使用
$0666与O_CREAT 一起打开并让您的umask设置将其屏蔽为0664或0644,根据用户的选择。您通常只对私人文件使用更有限的权限(例如写入您的电子邮件邮箱的程序,或加密保存的密码),它可能会使用0600权限创建文件。
标签: linux x86-64 gnu-assembler