Here is the question from assessed exercise as below. Even though I found and summarize the answer from the web, I still don't figure out how it works in Java programming which can assist a fresh learner to understand the logic and hence I am more likely to convert it to assembly language in Sigma 16 system.
Q: A Sigma16 system has an array, X , of 16-bit signed numbers in memory. Write an assembly language program to write a second array, Y whose ith element is 0 if the ith element of X is even and 1 if it is odd.
Ans:
ADD R1, R0, R0 ; i = 0
LEA R2, 4[R0] ; R2 = 4
LEA R3, 1[R0] ; R3 = 1
LEA R4, 2[R0] ; R4 = 2
Loop LOAD R5, X[R1] ; R5 = x[i]
DIV R6, R5, R4 ; x[i] / 2
STORE R15, Y[R1] ; y[i] = x[i] mod 2
ADD R1, R1, R3 ; i++
CMPLT R15, R1, R2 ; If i< 4 then⋯
JUMPT R15, Loop[R0] ; Loop
TRAP R0,R0,R0
Your answers are highly appreciated. Please also correct the above codes if they are way biased with errors.
16-bit signed integers in Java are short
s, so an equivalent code fragment in Java might be:
short[]x = new short[]{32, 203, 98, 19, 47, 78};
short[]y = new short[x.length];
for (int i = 0; i < x.length; ++x)
if (x[i] % 2 == 0) // % is the remainder operator
y[i] = 0;
else
y[i] = 1;
You could also do the odd-even test in one line with
y[i] = (x[i] % 2 == 0) ? 0 : 1;
And rather than a "remainder-after-division-by-2" test, there is also the option of a more assembly-like bitwise operation:
y[i] = x[i] & 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.