草庐IT

windows - 程序集:如何更改此代码以请求用户输入

coder 2024-06-11 原文

我最近在 Windows 10 笔记本电脑上用 visual studio 2017 编写了我的汇编程序。我现在希望更改此代码,将从用户获得的值放入注册表 eax、ebx、ecx 和 edx

\我已经让程序使用默认的硬编码值,但我很难在网络上找到任何东西来帮助我获得用户输入。该任务指定我必须使用程序集询问用户

.586                            ;Enables assembly on non Priiliged intructions for the prntium processor
.model flat ,c                  ;model=Initialises the program memory mode, flat=Identifies the size of code and data pointers and 
                            ;c=  identifies the naming and calling coventions
.stack 100h
.data                           ; This section will contain all of the static variables for our program
foo dd 0                    ;Variable to be used to store into meory   
.code                           ; Assembly code will be placed here

multi proc                      ; Start of the doit process. Like a method in C#. Method is called 
                            ;in the visual studio form Source

mov eax, 8                  ; Moves the value 8 into the eax Registry
mov ebx, 4                  ; Moves the value 4 into the ebx Registry
mov ecx, 6                  ; Moves the value 6 into the ecx Registry
mov edx, 12                 ; Moves the value 12 into the edx Registry

add eax, ebx                ; Adds the value stored in registry ebx to the vale in eax and stores the answer in eax
add eax, edx                ; Adds the value stored in registry edx to the vale in eax and stores the answer in eax
sub eax, ecx                ; subtracts the value stored in registry ecx from the vale in eax and stores the answer in eax
mul ebx                     ; Multiply the value in registry eax with the value in eax and stores the answer in eax
mov [foo], eax              ; stores the value in registry in eax into the computer memory


ret                         ; returns the valie of the accumulator
multi endp                      ; End of the doit method

end     

这是我用来从 visual studio 调用它的代码

#include <iostream>

extern "C" int multi();

void main()
{
printf("%d%",multi());
    std:getchar();
}

我现在需要帮助来更改我的代码以允许用户输入,我感觉我可能必须执行系统 ​​cll,但不确定是哪一个。这实际上是我第一天进行组装,因此我们将不胜感激

最佳答案

是的,您需要使用系统调用。在 C++ 中,您将调用 std::getchar() 从标准输入中读取一个字符。如果您被允许使用 C++ 标准库,只要您从汇编中调用它,那么代码将如下所示:

multi proc
    push   esi                   ; \ preserve
    push   ebx                   ; |  callee-preserve
    push   edi                   ; / registers

    call   _getchar              ; read input; return result in EAX
    mov    esi, eax              ; ESI = EAX
    sub    esi, 48               ; ESI -= '0'

    call   _getchar              ; read input; return result in EAX
    mov    ebx, eax              ; EBX = EAX
    sub    ebx, 48               ; EBX -= '0'

    call   _getchar              ; read input; return result in EAX
    mov    edi, eax              ; EDI = EAX
    sub    edi, 48               ; EDI -= '0'

    call   _getchar              ; read input; return result in EAX
    mov    edx, eax              ; EDX = EAX
    sub    edx, 48               ; EDX -= '0'

    mov    ecx, edi              ; ECX = EDI
    mov    eax, esi              ; EAX = ESI

    add    eax, ebx              ; EAX += EBX
    add    eax, edx              ; EAX += EDX
    sub    eax, ecx              ; EAX -= ECX
    mul    ebx                   ; EDX:EAX = EAX * EBX
    mov    [foo], eax            ; *foo = EAX

    pop    edi                   ; \ restore
    pop    ebx                   ; |  callee-preserve
    pop    esi                   ; /  registers

    ret
multi endp

调用getchar 函数非常简单。由于它不带任何参数,因此您无需担心传递任何内容。它在 EAX 寄存器中返回其结果,就像 x86 上的所有函数一样。

getchar 的返回值是用户输入字符的 ASCII 码。如果你想要一个数值,那么你需要从 ASCII 码中减去 '0',利用数字 0 到 9 在 ASCII 表中是连续的这一事实。

但是,您需要将结果存储在多次调用 getchar 的某处,因为 x86 调用约定指定 EAXEDX , 和 ECX 寄存器会被函数调用破坏(覆盖)。由于 ESIEBXEDI 是调用保留的,我将它们用作临时寄存器。另一种选择是使用堆栈来临时存储输入值。或者,优化代码以进行算术运算。

哦,请注意,虽然函数的名称在 C 代码中是 getchar,但当我们从汇编中调用它时,它是 _getchar。那是因为微软的编译器prepends an underscore to exported symbol names .

专业程序员会向此代码添加一些条件测试以检查错误。回想一下,getchar 在失败时返回 EOF (-1)。您可能还想处理用户在未输入数字的情况下按下 Enter 键的情况。您可以使用等效的 while 循环 (cmp eax, -1 + je) 来保持旋转直到 getchar 返回一个您认为在范围内的值(例如从 '0' 到 '9')。

考虑(警告:完全未经测试!):

ReadInteger proc
TryAgain:
    call    _getchar          ; read input from stdin; return result in EAX
    cmp     eax, 48           ; \ if (input < '0')
    jl      TryAgain          ; /  jump to TryAgain
    cmp     eax, 57           ; \ if (input > '9')
    jg      TryAgain          ; /  jump to TryAgain
    sub     eax, 48           ; input -= '0'
    ret
ReadInteger endp

multi proc
    push   esi
    push   ebx
    push   edi

    call   ReadInteger
    mov    esi, eax

    call   ReadInteger
    mov    ebx, eax

    call   ReadInteger
    mov    edi, eax

    call   ReadInteger
    add    eax, esi
    add    eax, ebx
    sub    eax, edi
    mul    ebx
    mov    [foo], eax

    pop    edi
    pop    ebx
    pop    esi

    ret
multi endp

如果你不能使用C++标准库而被迫使用操作系统调用,那么这就变得困难多了。我怀疑,这比你的导师期望你在这个阶段能够做到的要难得多。您需要调用一个 Win32 函数,如 ReadConsoleInput。不过这里有一个技巧:编写函数 C(或 C++),用 /Fa option 编译它。 ,并查看编译器生成的汇编列表。

关于windows - 程序集:如何更改此代码以请求用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657639/

有关windows - 程序集:如何更改此代码以请求用户输入的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  4. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  5. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  6. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  7. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  8. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  9. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  10. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

随机推荐