0

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.

  • 2
    Java has bitwise ops for integers so you can still test the low bit to see if an integer is even. `oddeven[i] = x[i] & 1;`. IDK what architecture that asm is for, but it's horrible. Don't use `div` to divide by 2, just shift or check the low bit. – Peter Cordes Jul 16 '18 at 23:38
  • 1
    Is `DIV` a "divide" operation or a "mod" (remainder) operation? Let's say it's division, and `DIV R6,R5,R4` means "divide R5 (x[i]) by R4 (2) and put the quotient in R6". Does the remainder magically go into R15? If not, the following `STORE R15, Y[R1]` is just storing junk into y[i]. Because the _remainder_ after division by 2 is what you'd want to store in the y array. – Kevin Anderson Jul 17 '18 at 00:30
  • @KevinAnderson: I found an example of asm using similar syntax in https://pdfs.semanticscholar.org/presentation/8247/8cbde9b3687cda6327f24f11750ab963bf58.pdf, just described as "MIPS-like" (as opposed to the dataflow architecture the slides are about). I found it with google on `"STORE" "R15" "DIV" "LOAD"`. R15 doesn't appear to be special for `DIV` there, but I couldn't find any ISA reference. There are some toy architectures with only add, no bitwise stuff, so a toy ISA with more arithmetic but still no bitwise is maybe plausible for students that are learning asm without binary... – Peter Cordes Jul 17 '18 at 00:54
  • @PeterCordes I was thinking it looked rather Alpha-like, but that's just the architecture _I_ happen to have some passing familiarity with... – Kevin Anderson Jul 17 '18 at 01:13
  • @KevinAnderson: Yeah, compare-into-register is like MIPS or Alpha, but Alpha's load/store mnemonics are `ldw` / `stw` (and it famously [didn't even *have* 16 or 8-bit loads/stores in the first 2 versions of the ISA](https://cs.stackexchange.com/questions/42765/)). Also, Alpha doesn't have a `trap` instruction or `lea`. (https://www2.cs.arizona.edu/projects/alto/Doc/local/alpha.instruction.html). The `jumpt` target address is very unusual, apparently an offset from a register instead of a simple relative jump? But yeah, probably there's a different instruction for remainder. – Peter Cordes Jul 17 '18 at 01:22

1 Answers1

0

16-bit signed integers in Java are shorts, 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;
Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21
  • 1
    [Mod in Java produces negative numbers](https://stackoverflow.com/q/5385024), same as C and most languages other than Python. (`-1 % 2 == -1`). The OP wants strictly 0 or 1 for even / odd, so you actually need to *avoid* signed division / modulo semantics. The easiest way is to do `x[i] & 1` like you would in asm, otherwise you could maybe use `x[i] % 2 == 0` as a condition to select 0 or 1 and let the optimizer turn it into a bitwise AND. The OP does specifically say their numbers are signed, but casting to unsigned would work, too, I think. – Peter Cordes Jul 17 '18 at 00:38
  • Well that's Python for you @PeterCordes: always being different just be to be different (;->). Anyway, you're right; I'll amend my answer. – Kevin Anderson Jul 17 '18 at 01:20
  • hi guys, thanks so much!!! It is quite brief and easy to understand thanks to your explanation with alternative approaches. – gettingtothebottom Aug 19 '18 at 11:52