编写返回数字是否为奇数的非常基本的 SPARC 汇编例程时遇到问题

发布于 2024-12-08 20:12:39 字数 683 浏览 0 评论 0原文

我正在编写一个名为 isOdd 的小型汇编例程,顾名思义,如果传递的整数为奇数,则通过从 % 操作返回 1 来返回。

到目前为止,这是我的代码:

Function prototype: int isOdd( long num )

isOdd:
    save     %sp, -96, %sp  ! Save caller's window

    mov      %i0, %o0       ! Parameter num goes to %o0
    mov      2, %l0         ! 2 goes to local register
    call     .rem           ! Call modulus subroutine
    nop

    mov      %o0, %l0       ! moves the result of the subroutine 
                            ! to output register o0
    ret
    restore

但是,我没有得到好的输出;事实上,它似乎只是返回我传递给 num 的任何值,而不是实际执行模数运算。

谷歌尚未证明对这样一个基本问题有帮助。这是我的第一个汇编代码,所以我对“寄存器”的概念非常不熟悉,我认为将它们混合在一起可能是我的错误所在。

预先感谢您的帮助!

I'm writing a small assembly routine called isOdd, which, as the name implies, returns if the passed integer is odd, by returning 1 from a % operation.

This is my code so far:

Function prototype: int isOdd( long num )

isOdd:
    save     %sp, -96, %sp  ! Save caller's window

    mov      %i0, %o0       ! Parameter num goes to %o0
    mov      2, %l0         ! 2 goes to local register
    call     .rem           ! Call modulus subroutine
    nop

    mov      %o0, %l0       ! moves the result of the subroutine 
                            ! to output register o0
    ret
    restore

However, I don't get good output; in fact, it seems like it is just returning whatever value I pass to num, instead of actually doing the modulus operation.

Google hasn't proved helpful for such a basic question. This is my first assembly code, so I'm pretty unfamiliar with the concept of "registers," and I think mixing them up is where my error may lie.

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-12-15 20:12:39

有一大堆寄存器,您可以将它们视为块中
8 个。在任意时刻,三个连续的 8 个寄存器块可见为
当前寄存器窗口,标记为%o0-%o7%l0-%l7,以及
%i0-%i7。 (第四个块有 8 个寄存器,%g0-%g7,它们是
全局而不是窗口排列的一部分。)

当您保存恢复时,窗口将移动两个个8块。
重叠块允许参数和结果传递。寄存器
调用者中名为 %o0-%o7 的内容与名为的内容相同
被调用者中的 %i0-%i7。 (被调用方中的两个新块是%l0-%l7
这些是该窗口内本地使用的私有内容,并且 %o0-%o7
当被调用者想要调用另一个函数时可以使用它。)

用图片更清楚:

:                      :
+----------------------+
| Block of 8 registers |      caller's window
+----------------------+  +----------------------+
| Block of 8 registers |  |      %i0 - %i7       |    ---------.
+----------------------+  +----------------------+             | save
| Block of 8 registers |  |      %l0 - %l7       |             v
+----------------------+  +----------------------+  +----------------------+
| Block of 8 registers |  |      %o0 - %o7       |  |      %i0 - %i7       |
+----------------------+  +----------------------+  +----------------------+
| Block of 8 registers |              ^             |      %l0 - %l7       |
+----------------------+      restore |             +----------------------+
| Block of 8 registers |              `---------    |      %o0 - %o7       |
+----------------------+                            +----------------------+
| Block of 8 registers |                                callee's window
+----------------------+
:                      :

您的调用者将 num 参数放入 %o0 (在其窗口中),然后来电
你。您保存以设置一个新窗口,因此您可以在%i0中看到它
窗户。

.rem 有两个参数。您将它们放在 %o0%o1 中(在您的
窗口),然后调用它。它将在其 %i0%i1 中看到它们(假设它确实
save 以设置新窗口)。它将答案放入其 %i0 中,即
你的%o0

同样,您应该将结果放入 %i0;给你打电话的人都会看到
在他们的 %o0 中。

There are a whole bunch of registers, which you can think of as being in blocks
of 8. At any one time, three consecutive blocks of 8 registers are visible as
the current register window, and are labelled as %o0-%o7, %l0-%l7, and
%i0-%i7. (There is a fourth block of 8 registers, %g0-%g7, which are
global rather than being a part of the windowing arrangement.)

When you save or restore, the window moves by two blocks of 8. The
overlapping block allows for parameter and result passing. The registers
which are named %o0-%o7 in the caller are the same ones that are named
%i0-%i7 in the callee. (The two new blocks in the callee are %l0-%l7,
which are private for local use within that window, and %o0-%o7 which the
callee can use when it in turn wants to call another function.)

It's clearer with a picture:

:                      :
+----------------------+
| Block of 8 registers |      caller's window
+----------------------+  +----------------------+
| Block of 8 registers |  |      %i0 - %i7       |    ---------.
+----------------------+  +----------------------+             | save
| Block of 8 registers |  |      %l0 - %l7       |             v
+----------------------+  +----------------------+  +----------------------+
| Block of 8 registers |  |      %o0 - %o7       |  |      %i0 - %i7       |
+----------------------+  +----------------------+  +----------------------+
| Block of 8 registers |              ^             |      %l0 - %l7       |
+----------------------+      restore |             +----------------------+
| Block of 8 registers |              `---------    |      %o0 - %o7       |
+----------------------+                            +----------------------+
| Block of 8 registers |                                callee's window
+----------------------+
:                      :

Your caller places the num argument into %o0 (in its window), then calls
you. You save to set up a new window, and so you see it in %i0 in your
window.

.rem takes two parameters. You place these in your %o0 and %o1 (in your
window), then call it. It will see them in its %i0 and %i1 (assuming it does
a save to set up a new window). It puts the answer in its %i0, which is
your %o0.

Similarly, you should put your result in your %i0; whoever called you will see it
in their %o0.

柠檬心 2024-12-15 20:12:39
! modified based on comments 

isOdd:
  save     %sp, -96, %sp  ! Save caller's window
  mov      %i0, %o0       ! Parameter num goes to %o0
  mov      2, %o1         ! 2 goes to %o1
  call     .rem           ! Call modulus subroutine
  nop

  mov      %o0, %i0       ! moves the result of the subroutine 
                          ! to input register i0
  ret
  restore
! modified based on comments 

isOdd:
  save     %sp, -96, %sp  ! Save caller's window
  mov      %i0, %o0       ! Parameter num goes to %o0
  mov      2, %o1         ! 2 goes to %o1
  call     .rem           ! Call modulus subroutine
  nop

  mov      %o0, %i0       ! moves the result of the subroutine 
                          ! to input register i0
  ret
  restore
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文