首页 > 解决方案 > 在汇编中有效地存储数据并使用它进行计算

问题描述

最近我开始学习组装,这是一段旅程!:) 我知道一些基本的东西,我想通过重新创建我的 python 程序来了解数据存储。该程序应该接受一个输入(我不确定在组装中是否可能)并返回九头蛇在一定天数后将拥有的头数。公式:result = ((old_day*new_day)+(new_day-old_day)) 我已将我的 python 代码简化为:

old_day = 2
new_day = 3
fight = 5

temp1 = 0
temp2 = 0
temp3 = 0

for i in range(fight):
    temp1 = old_day*new_day
    temp2 = new_day-old_day
    temp3 = temp1+temp2
    old_day = new_day
    new_day = temp3

print(old_day)

到目前为止,我在汇编中提出了这个:

global _start

section .data
    old dw 2
    l_old equ $ - old
    new dw 3
    l_new equ $ - new
    tem1 dw 0
    l_tem1 equ $ - tem1
    tem2 dw 0
    l_tem2 equ $ - tem2
    tem3 dw 0
    l_tem3 equ $ - tem3
    days dw 4


section .text
_start:
    mov al, [old]
    mov bl, [new]
    mov cl, [tem1]
    mov dl, [tem2]
    mov ah, [days]

    mov eax, 1
    mov ebx, 0
    int 0x80

label:

我被困在标签上,因为mul将其结果存储在其中,aex但我将我的值存储在aland中ah。我的问题是:会mul覆盖我的数据吗?有没有更好的数据存储方式?最后,我是否需要所有行,equ或者仅在输出时才相关?感谢您的时间和帮助:)

标签: assemblyx86nasm

解决方案


推荐阅读