RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1247499
Accepted
EvilBDSM
EvilBDSM
Asked:2022-02-23 06:19:09 +0000 UTC2022-02-23 06:19:09 +0000 UTC 2022-02-23 06:19:09 +0000 UTC

如何编写一个计算公式的程序?

  • 772

我试图制作一个程序来计算这个公式的值:

(4b + c - d) (2b + 5(3c + 5a)) / (4ab + 5c - 8d)

我试着至少写点东西,但绝对没有任何反应。该代码完全是胡说八道,甚至无法编译。

问题。如何编写正确的代码?我不知道这是不是一个愚蠢的问题......

Data segment
Mess db 'Результат : ', '$'
a  dw 0
b  dw 0
c  dw 0
d  dw 0
Data ends

Code segment use16
assume cs: Code, ds: Data
Start:
mov ax, Data
mov ds, ax

PrtChr '>'
InputInt a

PrtChr '>'
InputInt b

PrtChr '>'
InputInt c

PrtChr '>'
InputInt d

Print Mess, 0
 
mov ax, b        ; ax = b
mov cx, 4        ; cx = 4
imul cx;         ; b * 4
add cx, c        ; b * 4 + c
sub cx, d        ; b * 4 + c - d

mov ax, 2        ; ax = 2
mov al, b        ; al = b
imul al          ; b * 2
mov ax, 5        ; ax = 5
 
mov cl, 3        ; cl = 3
mov ch, c        ; ch = c
imul ch          ; c * 3
mov ecx, 5       ; ecx = 5
mov eax, a       ; eax = a
imul eax         ; a * 5
add ch, eax      ; ch = (3*c + 5*a)

imul ch          ; 5 * (3 * c + 5 * a)
add al, ch       ; (2 * b + (5 * ( 3 * c + 5 * a ) ) )

move ah, cx      ; ah = b * 4 + c - d
move eax, al     ; eax = (2 * b + (5 * ( 3 * c + 5 * a ) ) )
imul eax         ; eax = (b * 4 + c - d) * (2 * b + (5 * ( 3 * c + 5 * a ) ) )

; 4 * a * b
move ax, 4       ; ax = 4
move ah, a       ; ah = a
imul ah          ; 4 * a
move ah, ah
move ax, b       ; ax = b
imul ax          ; 4 * a * b

; 5 * c
move ah, 5
move al, c
imul al

; 4 * a * b + 5 * c
add ah, al

; 8 * d
move al, 8      ; al = 8
move ecx, d     ; ecx = d
imul ecx        ; d * 8

; 4 * a * b + 5 * c - 8 * d
sub ah, ecx

KeyPressed

mov ax, 4C00h
int 21h
Code ends

Stk segment STACK
dw 128 dup(?)
Stk ends
c
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    eanmos
    2022-02-23T12:29:08Z2022-02-23T12:29:08Z

    我会告诉你一个小秘密,它可以为你节省很多时间:

    如果您的任务是用汇编程序编写一些东西,那么您不需要用汇编程序编写它- 用 C 编写代码,然后编译并查看编译器生成的汇编程序代码。

    例如,在您的情况下,我们用 C 编写所需的程序:

    void
    calc(void)
    {
        /* Значения могут быть любыми. */
        int a = 2;
        int b = 3;
        int c = 5;
        int d = 7;
    
        int result = (4 * b + c - d) * (2 * b + 5 * (3 * c + 5 * a)) / (4 * a * b + 5 * c - 8 * d);
    }
    

    然后我们在关闭优化 ( -O0) 并发布汇编程序列表 ( -S) 的情况下对其进行编译。标志-fno-asynchronous-unwind-tables,-fno-dwarf2-cfi-asm并使汇编程序列表更清晰:

    # p.c — название файла с исходным кодом
    gcc -O0 -S -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm p.c
    

    在输出中,我们得到这样一个汇编程序列表:

        .file   "p.c"
        .text
        .globl  calc
        .type   calc, @function
    calc:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $2, -20(%rbp)
        movl    $3, -16(%rbp)
        movl    $5, -12(%rbp)
        movl    $7, -8(%rbp)
        movl    -16(%rbp), %eax
        leal    0(,%rax,4), %edx
        movl    -12(%rbp), %eax
        addl    %edx, %eax
        subl    -8(%rbp), %eax
        movl    %eax, %ecx
        movl    -16(%rbp), %eax
        leal    (%rax,%rax), %esi
        movl    -12(%rbp), %edx
        movl    %edx, %eax
        addl    %eax, %eax
        leal    (%rax,%rdx), %edi
        movl    -20(%rbp), %edx
        movl    %edx, %eax
        sall    $2, %eax
        addl    %edx, %eax
        leal    (%rdi,%rax), %edx
        movl    %edx, %eax
        sall    $2, %eax
        addl    %edx, %eax
        addl    %esi, %eax
        imull   %eax, %ecx
        movl    %ecx, %edx
        movl    -20(%rbp), %eax
        imull   -16(%rbp), %eax
        leal    0(,%rax,4), %esi
        movl    -12(%rbp), %ecx
        movl    %ecx, %eax
        sall    $2, %eax
        addl    %ecx, %eax
        leal    (%rsi,%rax), %ecx
        movl    -8(%rbp), %eax
        sall    $3, %eax
        subl    %eax, %ecx
        movl    %edx, %eax
        cltd
        idivl   %ecx
        movl    %eax, -4(%rbp)
        nop
        popq    %rbp
        ret
        .size   calc, .-calc
        .ident  "GCC: (GNU) 10.2.0"
        .section    .note.GNU-stack,"",@progbits
    

    有时使用Compiler Explorer之类的工具会更方便。


    如果你是为自己做,并且想学习如何用汇编程序编写,一切都是一样的。试着自己写程序,如果不行,用C写同样的代码,看看编译器把它翻译成什么,用什么汇编语言构造。

    尝试手动将 C 语言翻译成汇编程序:打印一份 C 代码并将汇编程序代码写在下一张纸上(当然,您可以不打印)。不需要翻译大型程序,您可以从小段代码开始,例如

    int a = 4;         // movl $4, -4(%rbp)
    int b = 7;         // movl $7, -8(%rbp)
                       // movl -4(%rbp), %edx
                       // movl -8(%rbp), %eax
                       // addl %edx, %eax
    int c = a + b;     // movl %eax, -12(%rbp)
    
    int a = 5;         //   movl $5, -4(%rbp)
                       //   jmp .L2
                       // .L3:
    while (a > 0) {    //   movl $0, %eax
        foo();         //   call foo
        a--;           //   subl $1, -4(%rbp)
    }                  // .L2:
                       //   cmpl $0, -4(%rbp)
                       //   jg .L3
    
    • 1

相关问题

  • free 出于某种原因不会从内存中删除数组

  • 请帮助代码

  • 为什么 masm 对字符串或文本文字太长发誓,为什么在结构中设置 db 或 dw?

  • 如何将数字拆分为位并将其写入 C 中的数组?

  • 如何以给定的角度移动物体?

  • 解决“子集和问题”的时效算法

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5