大会:从标准输入读取整数,增加它并打印到stdout [英] Assembly: Read integer from stdin, increment it and print to stdout

查看:220
本文介绍了大会:从标准输入读取整数,增加它并打印到stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I $ C $光盘IA32以下组件脚本。它应该读取从标准输入一个数字,增加它并打印到标准输出,但它并没有像预期的那样,它不会打印出任何东西(也许从标准输入读不终止或一些与印刷有误?)

  .section伪的.text
    .globl _start_开始:
    MOVL $ 3,%EAX#使用系统调用3(读)从标准输入读取
    MOVL $ 0%EBX#从标准输入读取(FD 0)
    MOVL%EDI,寄存器%EDI%ECX#店面输入
    MOVL $ 4,%#EDX读一个字节
    诠释$#0x80的调用系统调用从标准输入读取    包括EDI%递增#我们从标准输入获得值    MOVL $ 4,%EAX#使用系统调用4(写)打印到屏幕
    MOVL $ 1,EBX%#打印到标准输出(FD 1)
    MOVL%EDI,ECX%#指针文字写出来
    MOVL $ 4文字%EDX#长度写出来(1字节)
    诠释$#0x80的调用系统调用写入stdout    MOVL $ 1,%eax中使用#1系统调用(退出)退出
    MOVL $ 0%EBX#错误code = 0
    诠释$#0x80的调用系统调用

你看到错误?对于任何帮助,我感谢你在前进,

一切顺利,
西蒙


解决方案

  MOVL%EDI,ECX%在寄存器%EDI#店面输入
MOVL $ 4,%#EDX读一个字节

这部分是完全错误的。你不能存储在寄存器中读取结果。那是什么实际上做的是存储在包含在EDI%的地址,因为你还没有设置它,可能是什么地方你没有生意存储任何结果。首先,您需要在内存空间来存储的字符串。你还读四个字节,而不是之一。

我将取代像这样

  subl $ 4%ESP
MOVL%ESP,ECX%
MOVL $ 4%EDX

此将腾出空间栈上的4个字节,然后使用堆栈的顶部作为地址存储在字符串。你也有修改的参数为写系统调用来使用此地址。

这是你必须处理的另一个问题是,stdin和stdout通常处理文本,所以你在读什么将可能是一个字符串,而不是一个数字,用它作为一个数字,你就必须转换,然后将其转换回你写出来了。

I coded the following assembly script for IA32. It is supposed to read a number from stdin, increment it and print it to stdout, but it does not behave as expected, it doesn't print anything (maybe the reading from stdin does not terminate or something with the printing is wrong?)

.section .text
    .globl _start

_start:
    movl $3, %eax       # use syscall 3 (read) to read from stdin
    movl $0, %ebx       # reads from stdin (FD 0)
    movl %edi, %ecx    # store input in register %edi
    movl $4, %edx       # read one byte
    int  $0x80      # invoke system call to read from stdin

    incl %edi          # increment the value we got from stdin

    movl $4, %eax       # use syscall 4 (write) to print to screen
    movl $1, %ebx       # print to stdout (FD 1)
    movl %edi, %ecx    # pointer to text to write out
    movl $4, %edx       # length of text to write out (1 byte)
    int  $0x80          # invoke system call to write to stdout

    movl $1, %eax       # use syscall 1 (exit) to exit
    movl $0, %ebx       # error code = 0
    int  $0x80          # invoke system call

Do you see the error? For any help I thank you in advance,

All the best, Simon

解决方案

movl %edi, %ecx    # store input in register %edi
movl $4, %edx       # read one byte

This part is all wrong. You can't store the result of read in a register. What that's actually doing is storing the result at the address contained in %edi, which since you haven't set it, is probably somewhere you have no business storing anything. You first need to make room in memory to store the string at. You're also reading four bytes and not one.

I would replace that with something like this

subl $4, %esp
movl %esp, %ecx
movl $4, %edx

This will make room for 4 bytes on the stack, then use the top of the stack as the address to store the string at. You'll also have to modify the arguments for the write syscall to use this address.

Another problem that you'll have to deal with is that stdin and stdout usually deal with text, so what you're reading will probably be a string and not a number, to use it as a number you'll have to convert it and then convert it back before you write it out.

这篇关于大会:从标准输入读取整数,增加它并打印到stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆