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!
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 areglobal rather than being a part of the windowing arrangement.)
When you
saveorrestore, the window moves by two blocks of 8. Theoverlapping block allows for parameter and result passing. The registers
which are named
%o0–%o7in the caller are the same ones that are named%i0–%i7in the callee. (The two new blocks in the callee are%l0–%l7,which are private for local use within that window, and
%o0–%o7which thecallee can use when it in turn wants to call another function.)
It’s clearer with a picture:
Your caller places the
numargument into%o0(in its window), then callsyou. You
saveto set up a new window, and so you see it in%i0in yourwindow.
.remtakes two parameters. You place these in your%o0and%o1(in yourwindow), then call it. It will see them in its
%i0and%i1(assuming it doesa
saveto set up a new window). It puts the answer in its%i0, which isyour
%o0.Similarly, you should put your result in your
%i0; whoever called you will see itin their
%o0.