#JAS (java assembly) please help

4 messages · Page 1 of 1 (latest)

meager meadow
#

I need to do this:
Make a JAS program that does the following:

Read in x, y
print characters x, x + 2, x + 4, x + 6, ..., x + 2*y
You can assume that y is >= 0.

Do not use local variables (OP_ILOAD, OP_ISTORE),

currently i have this:
.main
IN
IN
SWAP

L1:
DUP
IFEQ END
SWAP
DUP
OUT
BIPUSH 2
IADD
SWAP
BIPUSH 1
ISUB
GOTO L1

END:
IADD
OUT
HALT
.end-main

which only works for 1 input

Input:
!!
Expected output:
!#%')+-/13579;=?ACEGIKMOQSUWY[]_ac
Actual output:
!#%')+-/13579;=?ACEGIKMOQSUWY[]_ac

but not for other inputs like:
Input:
#+
Expected output:
#%')+-/13579;=?ACEGIKMOQSUWY[]_acegikmoqsuwy
Actual output:
+-/13579;=?ACEGIKMOQSUWY[]_acegikmoq

what do i do...

elder tundra
#

learn real java

#

Javascript

crude isle
#

Never worked with JAS but I'd guess something like this might work:

.main
  IN          ; Read in x, stack is [x]
  DUP         ; Duplicate, stack is [x, x]
  IN          ; Read in y, stack is [x, x, y]
  BIPUSH 2    ; Push 2 for multiplication, stack is [x, x, y, 2]
  IMUL        ; multiply, stack is [x, x, 2y]
  IADD        ; stack is now [x, 2y + x]
  SWAP        ; stack is now [2y + x, x]

L1:
  DUP2        ; dup top two values, stack is [2y + x, x, 2y + x, x]
  ISUB        ; subtract to check if x exceeds 2y + x, stack is [2y + x, x, result]
  IFLT END    ; If result < 0, exit loop, stack is [2y + x, x]
  DUP         ; duplicate x for printing, stack is [2y + x, x, x]
  OUT         ; print, stack is [2y + x, x]
  BIPUSH 2    ; push 2 for addition, stack is [2y + x, x, 2]
  IADD        ; add x and 2, stack is [2y + x, x+2]
  GOTO L1     ; repeat loop

END:
  HALT        ; we're done
.end-main