【问题标题】:Questions about registers in Assembly Language关于汇编语言中寄存器的问题
【发布时间】:2017-11-15 09:02:48
【问题描述】:

对于像这样的代码:

;; Data definitions go here
.section .data
n1:     .byte 12
n2:     .byte 34
n3:     .byte 21
n4:     .byte 10
result: .space 1

;; Code definition goes here
.section .text
    .global main

main:
    push R17

    ldi R26, lo8(n1)        ; Loading the address of n1 in X
        ldi R27, hi8(n1)

        ld R24, X+              ; Load n1 in R24 and increase address
        ld R17, X+              ; Load n2 in R17 and increase address
        add R24, R17            ; R4 = n1 + n2
        ld R17, X+              ; Load n3 in R17 and increase address
        add R24, R17            ; R4 = n1 + n2 + n3
        ld R17, X+              ; Load n4 in R17 and increase address
        add R24, R17            ; Final result in R24 = n1 + n2 + n3 + n4

        ;; At this point R27:R26 contains the address of result
        st X, R24               ; Store the result

    ;; The result of this function is returned in R25:R24
        clr R25         ; So that R25:R24 has the result in 16 bits.

    pop R17
    ret
.end

1) 即使我们使用 R18 代替 R17 并移除弹出和推送 R17,结果是否相同?

2) 另外,只是做 clr R25 将 R25 连接到 R24? (因此制作 R25:R24?)

3) 此外,当执行如下指令操作时:

ADD R17, R18, ADD 是指令,R17 & R18 是寄存器吗?它们存储在 AVR 架构中的什么位置?

4) 最后,为什么将结果保存在 R25:R24 中,而不只是返回 R24 的值(包含 n1 + n2 + n3 + n4)?

我对汇编语言真的很陌生...请告诉我...任何帮助都会很棒!谢谢

【问题讨论】:

    标签: assembly computer-science avr


    【解决方案1】:
    1. 是的,但大概 R18 也必须根据调用约定保存
    2. 这只是零扩展。
    3. 是的。它们在 CPU 内部,但也映射到 SRAM 内存地址范围 0-31。
    4. 大概是因为函数预期返回一个 16 位的值。请注意,最好也执行 16 位加法。

    【讨论】:

    • 谢谢小丑!程序是否总是必须返回 16 位值?另外,你能告诉我关于零扩展的事情吗?谢谢!
    • main 的返回类型为int,因此您可以返回任何大小。我假设它是16位。零扩展只是用零填充最高位。您可以将它与无符号数字一起使用以转换为更大的数据类型。
    猜你喜欢
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多