简单的上卡西弗无限循环

发布于 2024-10-27 13:28:02 字数 1550 浏览 4 评论 0原文

简单的 upper-casifier 无限循环

我的代码有什么问题吗?

有什么建议吗?

我的编程环境是linux、emacs、汇编、at&t语法

.section .data
    .section .bss
    .lcomm buffer,1
    .section .text
    .global _start
_start:
    movl %esp,%ebp
    subl $8,%esp
    #8(%ebp) is 2nd arg == input
    #12(%ebp) is 3rd arg == output

    #open,read,open,write,close
    movl $5,%eax
    movl 8(%ebp),%ebx
    movl $0,%ecx
    movl $0666,%edx
    int $0x80
    #%eax contains input's fd
    #movl to first local var
    movl %eax,-4(%ebp)

    movl $5,%eax
    movl 12(%ebp),%ebx
    movl $03101,%ecx
    movl $0666,%edx
    int $0x80
    #eax contains output's fd
    #movl to second local var
    movl %eax,-8(%ebp)
loop:           
    #read 1 byte from file 1st byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80
    #buffer contains 1 byte of file
    cmpb $0,buffer
    je program_exit

    pushl buffer
    call convert    #will return converted to %al
    addl $4,%esp
    movb %al,buffer

    #write 1 byte from buffer to file
    movl $1,%edx
    movl $buffer,%ecx
    movl -8(%ebp),%ebx
    movl $4,%eax
    int $0x80
    jmp loop
program_exit:
    movl buffer,%ebx
    movl $1,%eax
    int $0x80

    .type convert,@function
convert:
    pushl %ebp
    movl %esp,%ebp
    movb 8(%ebp),%al #1 byte data in the buffer 
    cmpb $'a',%al
    jl convert_end
    cmpb $'z',%al
    jg convert_end
    addb $32,%al #convert to upper

convert_end:
    movl %ebp,%esp
    popl %ebp
    ret

simple upper-casifier loop infinitely

what's wrong with my code?

any advice?

my programming environment is linux, emacs, assembly, at&t syntax

.section .data
    .section .bss
    .lcomm buffer,1
    .section .text
    .global _start
_start:
    movl %esp,%ebp
    subl $8,%esp
    #8(%ebp) is 2nd arg == input
    #12(%ebp) is 3rd arg == output

    #open,read,open,write,close
    movl $5,%eax
    movl 8(%ebp),%ebx
    movl $0,%ecx
    movl $0666,%edx
    int $0x80
    #%eax contains input's fd
    #movl to first local var
    movl %eax,-4(%ebp)

    movl $5,%eax
    movl 12(%ebp),%ebx
    movl $03101,%ecx
    movl $0666,%edx
    int $0x80
    #eax contains output's fd
    #movl to second local var
    movl %eax,-8(%ebp)
loop:           
    #read 1 byte from file 1st byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80
    #buffer contains 1 byte of file
    cmpb $0,buffer
    je program_exit

    pushl buffer
    call convert    #will return converted to %al
    addl $4,%esp
    movb %al,buffer

    #write 1 byte from buffer to file
    movl $1,%edx
    movl $buffer,%ecx
    movl -8(%ebp),%ebx
    movl $4,%eax
    int $0x80
    jmp loop
program_exit:
    movl buffer,%ebx
    movl $1,%eax
    int $0x80

    .type convert,@function
convert:
    pushl %ebp
    movl %esp,%ebp
    movb 8(%ebp),%al #1 byte data in the buffer 
    cmpb 
a',%al
    jl convert_end
    cmpb 
z',%al
    jg convert_end
    addb $32,%al #convert to upper

convert_end:
    movl %ebp,%esp
    popl %ebp
    ret

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-11-03 13:28:02

请注意,read(2) 通过返回 0 发出文件结束条件信号。您试图通过查找 ascii NUL 来找到文件的结尾,这在 Unix 派生系统上非常罕见。 (如果您想要一种简单的方法来制作太字节大小的文件,dd if=/dev/zero of=/tmp/huge bs=1048576eek=1048576count=1。整个事情将是填充 ascii NUL 字符(或者整数 0,但是您想解释它。)

您需要修改代码以通过比较来找到文件结尾。 read(2) 系统调用的返回值,而不是通过查看接收到缓冲区的数据。

Note that read(2) signals an end-of-file condition by returning 0. You're trying to find the end of the file by looking for an ascii NUL, which is very rare on Unix-derived systems. (If you want an easy way to make a terabyte-sized file, dd if=/dev/zero of=/tmp/huge bs=1048576 seek=1048576 count=1. The whole thing will be filled with ascii NUL characters. (Or the integer 0, however you want to interpret it.)

You need to modify your code to find the end of file by comparing the return value of the read(2) system call, not by looking at the data you receive into your buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文