Add missing test files

This commit is contained in:
2018-05-26 15:35:58 +02:00
parent 7898c76570
commit 2fe47fe30c
82 changed files with 52716 additions and 2 deletions

4
.gitignore vendored
View File

@@ -1,6 +1,6 @@
target
*.jas
*.ijvm
/*.jas
/*.ijvm
*.conf
goJASM
rustijvm

BIN
files/advanced/Diamond.ijvm Normal file

Binary file not shown.

121
files/advanced/Diamond.jas Normal file
View File

@@ -0,0 +1,121 @@
// Recursive diamond by George Karlos
//
// print a pyramid of characters starting from input char and
// going down to ascii 0x31 (1) using nested recursions
//
.constant
NEWLINE 0xA
OBJREF 0xDEAD
ZERO 0x30
ONE 0x31
SPACE 0x20
.end-constant
.main
LDC_W OBJREF
prompt:
IN
DUP
LDC_W ZERO
ISUB
IFLT prompt
LDC_W ONE
INVOKEVIRTUAL diamond
done:
HALT
.end-main
.method diamond(n,i)
.var
backupn
backupi
.end-var
ILOAD n
LDC_W ZERO
SWAP
ISUB
IFEQ return
//print empty spaces
LDC_W OBJREF
LDC_W SPACE
ILOAD n
INVOKEVIRTUAL printntimes
//print char
LDC_W OBJREF
ILOAD n
ILOAD i
INVOKEVIRTUAL printntimes
LDC_W NEWLINE
OUT
ILOAD n
ISTORE backupn
IINC n -1
ILOAD i
ISTORE backupi
IINC i 2
LDC_W OBJREF
ILOAD n
ILOAD i
INVOKEVIRTUAL diamond
//dont print 1's again
ILOAD backupn
LDC_W ONE
ISUB
IFEQ return
//print empty spaces
LDC_W OBJREF
LDC_W SPACE
ILOAD backupn
INVOKEVIRTUAL printntimes
//print char
LDC_W OBJREF
ILOAD backupn
ILOAD backupi
INVOKEVIRTUAL printntimes
LDC_W NEWLINE
OUT
return:
BIPUSH 0x1 //Just a return value
IRETURN
.end-method
.method printntimes(c,n)
ILOAD n
LDC_W ZERO
SWAP
ISUB
IFEQ return
ILOAD c
OUT
IINC n -1
LDC_W OBJREF
ILOAD c
ILOAD n
INVOKEVIRTUAL printntimes
return:
BIPUSH 0x2 //Just a return value
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,303 @@
//SimpleCalc.jas by Ronald Bethlehem
//Includes the recursive function for factorial and printing a number
//Utilises reverse polish notation
//Also tests the SWAP and IF_ICMPEQ operators
//Can read larger numbers, all undefined characters delimit
//Valid example:
//99 5 + 4 / 22 1*- ! ? 99 5+4/22v1*-!?. (prints 24\n24\n)
.constant
OBJREF 0xDEAD
ZERO 0x30
NINE 0x39
FACTORIAL 0x21
DUPLICATE 0x26
MULTIPLY 0x2A
PLUS 0x2B
MINUS 0x2D
DIVIDE 0x2F
PRINT 0x3F
END 0x2E
.end-constant
.main
.var
mode //0 for end, 1 for numberreading, 2 for other
tmp
.end-var
// Add some things to the stack
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 2
ISTORE mode
readchar:
ILOAD mode
IFEQ end
IN
//Is is a number?
DUP
LDC_W ZERO
ISUB
IFLT continue
DUP
LDC_W NINE
BIPUSH 1
IADD
ISUB
IFLT read_number
//Set mode to not number (aka delimited)
continue:
BIPUSH 2
ISTORE mode
//Check for operators
DUP
LDC_W END
IF_ICMPEQ read_end
DUP
LDC_W FACTORIAL
IF_ICMPEQ read_factorial
DUP
LDC_W DUPLICATE
IF_ICMPEQ read_duplicate
DUP
LDC_W MULTIPLY
IF_ICMPEQ read_multiply
DUP
LDC_W PLUS
IF_ICMPEQ read_plus
DUP
LDC_W MINUS
IF_ICMPEQ read_minus
DUP
LDC_W DIVIDE
IF_ICMPEQ read_divide
DUP
LDC_W PRINT
IF_ICMPEQ read_print
//Ignore character
POP
GOTO readchar
read_end:
POP
BIPUSH 0
ISTORE mode
GOTO readchar
read_factorial:
POP
ISTORE tmp
LDC_W OBJREF
SWAP
ILOAD tmp
INVOKEVIRTUAL factorial
GOTO readchar
read_duplicate:
POP
DUP
GOTO readchar
read_multiply:
POP
ISTORE tmp
LDC_W OBJREF
SWAP
ILOAD tmp
INVOKEVIRTUAL mul
GOTO readchar
read_plus:
POP
IADD
GOTO readchar
read_minus:
POP
ISUB
GOTO readchar
read_divide:
POP
ISTORE tmp
LDC_W OBJREF
SWAP
ILOAD tmp
INVOKEVIRTUAL divide
GOTO readchar
read_print:
POP
LDC_W OBJREF
SWAP
INVOKEVIRTUAL printNumber
POP
BIPUSH 0xA //newline
OUT
GOTO readchar
read_number:
LDC_W ZERO
ISUB
ILOAD mode
BIPUSH 1
IF_ICMPEQ read_number_continue
BIPUSH 1
ISTORE mode
GOTO readchar
read_number_continue:
SWAP
LDC_W OBJREF
SWAP
BIPUSH 10
INVOKEVIRTUAL mul
IADD
GOTO readchar
end:
HALT
.end-main
.method printNumber(a)
ILOAD a
BIPUSH 10
ISUB
IFLT small
recurse:
LDC_W OBJREF
LDC_W OBJREF
ILOAD a
BIPUSH 10
INVOKEVIRTUAL mod
LDC_W OBJREF
LDC_W OBJREF
ILOAD a
BIPUSH 10
INVOKEVIRTUAL divide
INVOKEVIRTUAL printNumber
POP
INVOKEVIRTUAL printNum
//POP
IRETURN
small:
LDC_W OBJREF
ILOAD a
INVOKEVIRTUAL printNum
//POP
IRETURN
.end-method
.method printNum(a)
ILOAD a
LDC_W ZERO
IADD
OUT
BIPUSH 0x0
IRETURN
.end-method
.method mul(a,b)
ILOAD b
ILOAD a
ISUB
IFLT noswap
ILOAD a
ILOAD b
SWAP
ISTORE b
ISTORE a
noswap:
BIPUSH 0
loop:
ILOAD b
IFEQ bzero
bpos:
ILOAD b
BIPUSH 1
ISUB
ISTORE b
add:
ILOAD a
IADD
GOTO loop
bzero:
IRETURN
.end-method
.method divide(a,b)
.var
count
.end-var
ILOAD b
IFEQ err
BIPUSH 0
ISTORE count
ILOAD a
loop:
ILOAD a
ILOAD b
ISUB
DUP
IFLT done
ISTORE a
inc:
ILOAD count
BIPUSH 1
IADD
ISTORE count
GOTO loop
done:
ILOAD count
IRETURN
err:
ERR
.end-method
.method mod(a,b)
LDC_W OBJREF
LDC_W OBJREF
ILOAD a
ILOAD b
INVOKEVIRTUAL divide
ILOAD b
INVOKEVIRTUAL mul
ILOAD a
SWAP // yeah I know, I could also have pushed a at the start, but its a test, who gives a carp about performance
ISUB
IRETURN
.end-method
.method factorial(i)
.var
tmp
.end-var
LDC_W OBJREF
ILOAD i
ILOAD i
BIPUSH 1
ISUB
ISTORE tmp
ILOAD tmp
IFEQ one
LDC_W OBJREF
ILOAD tmp
INVOKEVIRTUAL factorial
INVOKEVIRTUAL mul
one:
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,489 @@
//
// Name
//
// ijvmtest.jas
//
// Description
//
// This program tests the IJVM instruction set.
//
// Author
//
// Andrew S. Tanenbaum February 16, 1999
//
// Modification History
//
// Ray Ontko April 11, 1999
// Added tests for LDC_W, ILOAD, ISTORE, INVOKEVIRTUAL and IRETURN
// Also added OUT instructions to help identify which ERROR occurred.
//
// Notes
//
// The INP instruction is not tested by this program.
// The ERR instruction is not tested by this program.
//
.constant
objref 0xCAFE // may be any value. Needed by invokevirtual.
my_max 100
.end-constant
.main
.var
my_var
.end-var
BIPUSH 19 // mark the bottom of the stack
BIPUSH 20 // # iterations
L1: BIPUSH 3
BIPUSH 3
IF_ICMPEQ L2
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L2: BIPUSH -1
BIPUSH -1
IF_ICMPEQ L3
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L3: BIPUSH 4 // start testing IADD
BIPUSH 7
IADD
BIPUSH 11
IF_ICMPEQ L4
BIPUSH 48
BIPUSH 3
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L4: BIPUSH -5
BIPUSH -9
IADD
BIPUSH -14
IF_ICMPEQ L5
BIPUSH 48
BIPUSH 4
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L5: BIPUSH 10 // start testing ISUB
BIPUSH 7
ISUB
BIPUSH 3
IF_ICMPEQ L6
BIPUSH 48
BIPUSH 5
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L6: BIPUSH -3
BIPUSH -7
ISUB
BIPUSH 4
IF_ICMPEQ L7
BIPUSH 48
BIPUSH 6
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L7: BIPUSH 0xF2 // start testing IAND
BIPUSH 0x31
IAND
BIPUSH 0x30
IF_ICMPEQ L8
BIPUSH 48
BIPUSH 7
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L8: BIPUSH 0xF2 // start testing IOR
BIPUSH 0x31
IOR
BIPUSH 0xF3
IF_ICMPEQ L9
BIPUSH 48
BIPUSH 8
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L9: BIPUSH 20 // start testing DUP
DUP
IADD
BIPUSH 40
IF_ICMPEQ L10
BIPUSH 48
BIPUSH 9
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L10: BIPUSH 32 // start testing POP
BIPUSH 17
POP
BIPUSH 32
IF_ICMPEQ L11
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 0
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L11: BIPUSH 9 // start test of IFLT
IFLT ERR
BIPUSH 0
IFLT ERR
BIPUSH -1
IFLT L12
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L12: BIPUSH 1 // start testing IFEQ
IFEQ ERR
BIPUSH 0
IFEQ L13
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L13: BIPUSH 16 // start testing SWAP
BIPUSH -5
SWAP
BIPUSH 16
IF_ICMPEQ L14
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 3
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L14: BIPUSH -5
IF_ICMPEQ L15
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 4
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L15: LDC_W my_max // start testing LDC_W
BIPUSH 100
IF_ICMPEQ L16
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 5
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L16: BIPUSH 83 // start testing ISTORE, IINC and ILOAD
ISTORE my_var
IINC my_var 4
BIPUSH 99
POP
ILOAD my_var
BIPUSH 87
IF_ICMPEQ L17
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 6
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L17: LDC_W objref // start testing INVOKEVIRTUAL and IRETURN
BIPUSH -1
BIPUSH -10
INVOKEVIRTUAL cmp
BIPUSH 1
IF_ICMPEQ L18
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 7
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L18: LDC_W objref
BIPUSH -10
BIPUSH -1
INVOKEVIRTUAL cmp
BIPUSH -1
IF_ICMPEQ L19
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 8
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L19: LDC_W objref
BIPUSH -10
BIPUSH -10
INVOKEVIRTUAL cmp
BIPUSH 0
IF_ICMPEQ L20
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 9
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L20: LDC_W objref
BIPUSH -10
BIPUSH 10
INVOKEVIRTUAL cmp
BIPUSH -1
IF_ICMPEQ L21
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 48
BIPUSH 0
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L21: LDC_W objref
BIPUSH 10
BIPUSH -10
INVOKEVIRTUAL cmp
BIPUSH 1
IF_ICMPEQ L22
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L22: LDC_W objref
BIPUSH 0
BIPUSH 0
INVOKEVIRTUAL cmp
BIPUSH 0
IF_ICMPEQ L23
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L23: LDC_W objref
BIPUSH 1
BIPUSH 10
INVOKEVIRTUAL cmp
BIPUSH -1
IF_ICMPEQ L24
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 48
BIPUSH 3
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L24: LDC_W objref
BIPUSH 10
BIPUSH 1
INVOKEVIRTUAL cmp
BIPUSH 1
IF_ICMPEQ L25
BIPUSH 48
BIPUSH 2
IADD
OUT
BIPUSH 48
BIPUSH 4
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L25: LDC_W objref
BIPUSH 10
BIPUSH 10
INVOKEVIRTUAL cmp
BIPUSH 0
IF_ICMPEQ L26
BIPUSH 48
BIPUSH 1
IADD
OUT
BIPUSH 48
BIPUSH 8
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
L26: NOP // test NOP
// iterate
BIPUSH 1
ISUB
DUP
IFEQ FINAL
GOTO L1
FINAL: POP // see if the marker is still there
BIPUSH 19
IF_ICMPEQ OK
BIPUSH 48
BIPUSH 0
IADD
OUT
BIPUSH 32
OUT
GOTO ERR
OK: BIPUSH 79
OUT
BIPUSH 75
OUT
HALT
ERR:
BIPUSH 69
OUT
BIPUSH 82
OUT
BIPUSH 82
OUT
BIPUSH 79
OUT
BIPUSH 82
OUT
HALT
.end-main
// cmp returns -1 if p1 < p2, 0 if p1 = p2, and 1 if p1 > p2
.method cmp(p1,p2)
.var
temp
.end-var
ILOAD p1
ILOAD p2
ISUB
ISTORE temp
ILOAD temp
IFLT lt
ILOAD temp
IFEQ eq
gt: BIPUSH 1
GOTO done
lt: BIPUSH -1
GOTO done
eq: BIPUSH 0
done: IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,20 @@
.constant
objref 0xCAFE // may be any value. Needed by invokevirtual.
.end-constant
.main
LDC_W objref
INVOKEVIRTUAL magic
.end-main
.method addone()
BIPUSH 0x0
IRETURN
.end-method
.method magic()
LDC_W objref
INVOKEVIRTUAL addone
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,36 @@
.constant
objref 0xCAFE // may be any value. Needed by invokevirtual.
a 5
b 15
.end-constant
.main
LDC_W objref
BIPUSH 0x1
LDC_W a
INVOKEVIRTUAL magic
NOP
.end-main
.method addone(var)
ILOAD var
BIPUSH 0x1
IADD
DUP
IRETURN
.end-method
.method magic(x, y)
LDC_W b
ILOAD x
ILOAD y
IADD
ISUB
DUP
LDC_W objref
SWAP
INVOKEVIRTUAL addone
DUP
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,523 @@
.main
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 0x0
INVOKEVIRTUAL test
.end-main
.method test()
.var
temp0
temp1
temp2
temp3
temp4
temp5
temp6
temp7
temp8
temp9
temp10
temp11
temp12
temp13
temp14
temp15
temp16
temp17
temp18
temp19
temp20
temp21
temp22
temp23
temp24
temp25
temp26
temp27
temp28
temp29
temp30
temp31
temp32
temp33
temp34
temp35
temp36
temp37
temp38
temp39
temp40
temp41
temp42
temp43
temp44
temp45
temp46
temp47
temp48
temp49
temp50
temp51
temp52
temp53
temp54
temp55
temp56
temp57
temp58
temp59
temp60
temp61
temp62
temp63
temp64
temp65
temp66
temp67
temp68
temp69
temp70
temp71
temp72
temp73
temp74
temp75
temp76
temp77
temp78
temp79
temp80
temp81
temp82
temp83
temp84
temp85
temp86
temp87
temp88
temp89
temp90
temp91
temp92
temp93
temp94
temp95
temp96
temp97
temp98
temp99
temp100
temp101
temp102
temp103
temp104
temp105
temp106
temp107
temp108
temp109
temp110
temp111
temp112
temp113
temp114
temp115
temp116
temp117
temp118
temp119
temp120
temp121
temp122
temp123
temp124
temp125
temp126
temp127
temp128
temp129
temp130
temp131
temp132
temp133
temp134
temp135
temp136
temp137
temp138
temp139
temp140
temp141
temp142
temp143
temp144
temp145
temp146
temp147
temp148
temp149
temp150
temp151
temp152
temp153
temp154
temp155
temp156
temp157
temp158
temp159
temp160
temp161
temp162
temp163
temp164
temp165
temp166
temp167
temp168
temp169
temp170
temp171
temp172
temp173
temp174
temp175
temp176
temp177
temp178
temp179
temp180
temp181
temp182
temp183
temp184
temp185
temp186
temp187
temp188
temp189
temp190
temp191
temp192
temp193
temp194
temp195
temp196
temp197
temp198
temp199
temp200
temp201
temp202
temp203
temp204
temp205
temp206
temp207
temp208
temp209
temp210
temp211
temp212
temp213
temp214
temp215
temp216
temp217
temp218
temp219
temp220
temp221
temp222
temp223
temp224
temp225
temp226
temp227
temp228
temp229
temp230
temp231
temp232
temp233
temp234
temp235
temp236
temp237
temp238
temp239
temp240
temp241
temp242
temp243
temp244
temp245
temp246
temp247
temp248
temp249
temp250
temp251
temp252
temp253
temp254
temp255
temp256
temp257
temp258
temp259
temp260
temp261
temp262
temp263
temp264
temp265
temp266
temp267
temp268
temp269
temp270
temp271
temp272
temp273
temp274
temp275
temp276
temp277
temp278
temp279
temp280
temp281
temp282
temp283
temp284
temp285
temp286
temp287
temp288
temp289
temp290
temp291
temp292
temp293
temp294
temp295
temp296
temp297
temp298
temp299
temp300
temp301
temp302
temp303
temp304
temp305
temp306
temp307
temp308
temp309
temp310
temp311
temp312
temp313
temp314
temp315
temp316
temp317
temp318
temp319
temp320
temp321
temp322
temp323
temp324
temp325
temp326
temp327
temp328
temp329
temp330
temp331
temp332
temp333
temp334
temp335
temp336
temp337
temp338
temp339
temp340
temp341
temp342
temp343
temp344
temp345
temp346
temp347
temp348
temp349
temp350
temp351
temp352
temp353
temp354
temp355
temp356
temp357
temp358
temp359
temp360
temp361
temp362
temp363
temp364
temp365
temp366
temp367
temp368
temp369
temp370
temp371
temp372
temp373
temp374
temp375
temp376
temp377
temp378
temp379
temp380
temp381
temp382
temp383
temp384
temp385
temp386
temp387
temp388
temp389
temp390
temp391
temp392
temp393
temp394
temp395
temp396
temp397
temp398
temp399
temp400
temp401
temp402
temp403
temp404
temp405
temp406
temp407
temp408
temp409
temp410
temp411
temp412
temp413
temp414
temp415
temp416
temp417
temp418
temp419
temp420
temp421
temp422
temp423
temp424
temp425
temp426
temp427
temp428
temp429
temp430
temp431
temp432
temp433
temp434
temp435
temp436
temp437
temp438
temp439
temp440
temp441
temp442
temp443
temp444
temp445
temp446
temp447
temp448
temp449
temp450
temp451
temp452
temp453
temp454
temp455
temp456
temp457
temp458
temp459
temp460
temp461
temp462
temp463
temp464
temp465
temp466
temp467
temp468
temp469
temp470
temp471
temp472
temp473
temp474
temp475
temp476
temp477
temp478
temp479
temp480
temp481
temp482
temp483
temp484
temp485
temp486
temp487
temp488
temp489
temp490
temp491
temp492
temp493
temp494
temp495
temp496
temp497
temp498
temp499
.end-var
BIPUSH 0x1
WIDE
ISTORE temp0
BIPUSH 0x2
WIDE
ISTORE temp256
WIDE
ILOAD temp0
WIDE
ILOAD temp256
HALT
.end-method

Binary file not shown.

32791
files/advanced/test-wide2.jas Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -0,0 +1,11 @@
.main
BIPUSH 0x1
L1:
BIPUSH 0x2
GOTO L1
BIPUSH 0x4
OUT
HALT
.end-main

Binary file not shown.

View File

@@ -0,0 +1,20 @@
.main
BIPUSH 0x1
BIPUSH 0x4
BIPUSH 0x3
BIPUSH 0x2
BIPUSH 0x2
BIPUSH 0x2
BIPUSH 0x2
BIPUSH 0x3
INVOKEVIRTUAL test
OUT
.end-main
.method test()
L1:
BIPUSH 0x2
GOTO L1
.end-method

BIN
files/bonus/bfi.ijvm Normal file

Binary file not shown.

529
files/bonus/bfi.jas Normal file
View File

@@ -0,0 +1,529 @@
// 888 .d888d8b
// 888 d88P" Y8P
// 888 888
// 88888b. 888888888
// 888 "88b888 888
// 888 888888 888
// 888 d88P888 888
// 88888P" 888 888
//
// bfi.jas, the brainfuck interpreter.
// by Arthur de Fluiter, 2017
// https://en.wikipedia.org/wiki/Brainfuck
//
// REQUIRES: Heap opeartions & simple invokevirtual
//
// Note: Since brainfuck has the ',' operator which
// reads from input and we only have one input for IJVM
// programs, we need to somehow pass both the program and
// input via stdin. This is done by delimiting the input
// with a ':'. Aka the following program will output 'd'
// ,+++.:a
//
// Note 2: This might be a difficult program to debug, especially
// given the fact that you interpret (brainfuck) on an interpreted (IJVM) platform.
.constant
OBJREF 0xdead
STACK_SIZE 0x100 // support for 160 levels of nested []
MEMORY_SIZE 0x1000 // size of the bfi's memory (needs to be a power of 2)
INIT_TEXT_SIZE 0x4 // initial size of the bfi program buffer
INIT_INPUT_SIZE 0x4 // initial size of the bfi input buffer
// configurable to conform with indecisive bf community
EOF 0
CELL_MIN 0 // adjustable to not upset the brainfuck community
CELL_MAX 255 // (they're not entirely sure whether signed/unsigned word/quadword)
// delimiter for seperating bf program and bf input
DELIMITER 0x3a // :
// characters used in brainfuck
ASCII_PLUS 0x2b // +
ASCII_MINUS 0x2d // -
ASCII_LT 0x3c // <
ASCII_GT 0x3e // >
ASCII_BO 0x5b // [
ASCII_BC 0x5d // ]
ASCII_DOT 0x2e // .
ASCII_COMMA 0x2c // ,
.end-constant
.main
.var
text // the bf program buffer
textcap // capacity of the program buffer
textsize // size of of the program buffer
input // the bf program's input
inputcap // capacity of the input
inputsize // size of of the input
char // read in char
.end-var
// first setting up the variables
BIPUSH 0x0 // textsize = 0
ISTORE textsize
LDC_W INIT_TEXT_SIZE // textcap = INIT_TEXT_SIZE
ISTORE textcap
LDC_W INIT_TEXT_SIZE // text = new array[INIT_TEXT_SIZE]
NEWARRAY
ISTORE text
BIPUSH 0x0 // inputsize = 0
ISTORE inputsize
LDC_W INIT_INPUT_SIZE // inputcap = INIT_TEXT_SIZE
ISTORE inputcap
LDC_W INIT_INPUT_SIZE // input = new array[INIT_INPUT_SIZE]
NEWARRAY
ISTORE input
slurp_program_loop:
// if (textsize == text capacity) -> realloc text
ILOAD textsize
ILOAD textcap
ISUB
IFEQ text_grow
// char = IN;
IN
ISTORE char
// if (char == 0) -> we're done reading
ILOAD char
IFEQ done
// if (char == DELIMETER) -> read input
ILOAD char
LDC_W DELIMITER
IF_ICMPEQ slurp_input_loop
// if (!checkBF(char)) goto next char, aka eliminating unnecessary chars
LDC_W OBJREF
ILOAD char
INVOKEVIRTUAL checkBF
IFEQ slurp_program_loop
// text[textsize++] = char
ILOAD char
ILOAD textsize
IINC textsize 0x1
ILOAD text
IASTORE
GOTO slurp_program_loop
slurp_input_loop:
// if (input size == input capacity) -> realloc input
ILOAD inputsize
ILOAD inputcap
ISUB
IFEQ input_grow
// char = IN; if (char == 0) -> we're done reading
IN
DUP
ISTORE char
IFEQ done
// input[inputsize++] = char
ILOAD char
ILOAD inputsize
IINC inputsize 0x1
ILOAD input
IASTORE
GOTO slurp_input_loop
text_grow:
// text = realloc(text, textcap, (textcap *= 2))
LDC_W OBJREF
ILOAD text
ILOAD textcap
DUP
DUP
IADD
DUP
ISTORE textcap
INVOKEVIRTUAL realloc
ISTORE text
GOTO slurp_program_loop
input_grow:
// input = realloc(input, inputcap, (inputcap *= 2))
LDC_W OBJREF
ILOAD input
ILOAD inputcap
DUP
DUP
IADD
DUP
ISTORE inputcap
INVOKEVIRTUAL realloc
ISTORE input
GOTO slurp_input_loop
done:
LDC_W OBJREF
ILOAD text
ILOAD textsize
ILOAD input
ILOAD inputsize
INVOKEVIRTUAL exec
POP
HALT
.end-main
// checkBF whether a char is a brainfuck symbol or not
// @param symbol
// @return 0 if not bf 1 if bf
.method checkBF(symbol)
ILOAD symbol
LDC_W ASCII_PLUS
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_MINUS
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_LT
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_GT
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_BO
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_BC
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_DOT
IF_ICMPEQ ret1
ILOAD symbol
LDC_W ASCII_COMMA
IF_ICMPEQ ret1
BIPUSH 0
IRETURN
ret1:
BIPUSH 1
IRETURN
.end-method
// realloc, 'reallocating' a buffer
// @param buffer, original buffer
// @param curSize, size to be copied
// @param newSize, size that it should become
//
// @return new buffer of newSize, with the contents of the last buffer
.method realloc(buffer, curSize, newSize)
.var
newBuffer
i
.end-var
ILOAD newSize
NEWARRAY
ISTORE newBuffer
BIPUSH 0
ISTORE i
loop:
ILOAD i
ILOAD curSize
IF_ICMPEQ end
ILOAD i
ILOAD buffer
IALOAD
ILOAD i
ILOAD newBuffer
IASTORE
IINC i 0x1
GOTO loop
end:
ILOAD newBuffer
IRETURN
.end-method
// exec, runs a brainfuck program
// @param text, program text (not containing anything but raw bf)
// @param textsize, program size
// @param input, the stdin of the program
// @param size of input
// @return garbage
.method exec(text, textsize, input, inputsize)
.var
pc // keeps track of where we are in the bf program
memory // the memory array
memptr // points to offset in the memory array
stack // keeps track of where to return bcs of []
stackptr // pointer to last one
instr // holds current instruction
inputptr // points to what input symbol we're at
tmp // used as temporary value in seeking operation
.end-var
BIPUSH 0 // pc = 0
ISTORE pc
BIPUSH 0 // inputptr = 0
ISTORE inputptr
BIPUSH 0 // memptr = 0
ISTORE memptr
LDC_W MEMORY_SIZE // memory = NEWARRAY(MEMORY_SIZE)
NEWARRAY
ISTORE memory
BIPUSH -1 // stackptr = -1
ISTORE stackptr
LDC_W STACK_SIZE // stack = NEWARRAY(STACK_SIZE)
NEWARRAY
ISTORE stack
// since the memory technically doesn't have to be initialised to 0, I put this in for safety
init_mem_loop:
LDC_W MEMORY_SIZE
ILOAD memptr
IF_ICMPEQ done_init_mem
BIPUSH 0
ILOAD memptr
ILOAD memory
IASTORE
IINC memptr 1
GOTO init_mem_loop
done_init_mem:
BIPUSH 0
ISTORE memptr
exec_loop:
// if we're at the end of the program, return
ILOAD pc
ILOAD textsize
IF_ICMPEQ done
// instr = text[pc++]
ILOAD pc
IINC pc 1
ILOAD text
IALOAD
ISTORE instr
ILOAD instr
LDC_W ASCII_PLUS
IF_ICMPEQ plus
ILOAD instr
LDC_W ASCII_MINUS
IF_ICMPEQ minus
ILOAD instr
LDC_W ASCII_DOT
IF_ICMPEQ dot
ILOAD instr
LDC_W ASCII_COMMA
IF_ICMPEQ comma
ILOAD instr
LDC_W ASCII_LT
IF_ICMPEQ lessthan
ILOAD instr
LDC_W ASCII_GT
IF_ICMPEQ greaterthan
ILOAD instr
LDC_W ASCII_BO
IF_ICMPEQ blockopen
ILOAD instr
LDC_W ASCII_BC
IF_ICMPEQ blockclose
GOTO exec_loop
plus:
ILOAD memptr
ILOAD memory
IALOAD
DUP
LDC_W CELL_MAX
IF_ICMPEQ plus_overflow
BIPUSH 1
IADD
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
plus_overflow:
POP
LDC_W CELL_MIN
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
minus:
ILOAD memptr
ILOAD memory
IALOAD
DUP
LDC_W CELL_MIN
IF_ICMPEQ minus_underflow
BIPUSH -1
IADD
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
minus_underflow:
POP
LDC_W CELL_MAX
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
dot:
ILOAD memptr
ILOAD memory
IALOAD
OUT
GOTO exec_loop
comma:
ILOAD inputptr
ILOAD inputsize
IF_ICMPEQ comma_noinput
// memory[memptr] = input[inputptr++]
ILOAD inputptr
IINC inputptr 1
ILOAD input
IALOAD
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
comma_noinput:
LDC_W EOF
ILOAD memptr
ILOAD memory
IASTORE
GOTO exec_loop
lessthan:
ILOAD memptr
BIPUSH -1
IADD
LDC_W MEMORY_SIZE
BIPUSH -1
IADD
IAND
ISTORE memptr
GOTO exec_loop
greaterthan:
ILOAD memptr
BIPUSH 1
IADD
LDC_W MEMORY_SIZE
BIPUSH -1
IADD
IAND
ISTORE memptr
GOTO exec_loop
blockopen:
// let's first check if we should seek to after the closing ]
ILOAD memptr
ILOAD memory
IALOAD
IFEQ blockopen_seek
// if not, we'll add pc to the return stack and continue normal execution
IINC stackptr 1
// push the address of the [ on stack, we'll jump back to blockopen
ILOAD pc // pc counter was already updated
BIPUSH -1 // so we need to subtract 1
IADD
ILOAD stackptr
ILOAD stack
IASTORE
// go back to normal execution loop
GOTO exec_loop
blockopen_seek:
// we increase tmp when a [ is encountered and decrease if a ] is encountered
// when tmp reaches 0, we'll have reached the correct one
BIPUSH 1
ISTORE tmp
blockopen_seekloop:
//
ILOAD pc
IINC pc 1
ILOAD program
IALOAD
ISTORE instr
// if instr == '[' goto inc
ILOAD instr
LDC_W ASCII_BO
IF_ICMP blockopen_inc
// if instr == ']' goto dec
ILOAD instr
LDC_W ASCII_BC
IF_ICMP blockopen_dec
// if it was neither a [ or ], just go to next instruction
GOTO blockopen_seekloop
blockopen_inc:
IINC tmp 1
GOTO blockopen_seekloop
blockopen_dec:
IINC tmp -1
IFEQ exec_loop
GOTO blockopen_seekloop
blockclose:
// we have to return to the last [
ILOAD stackptr
ILOAD stack
IALOAD
ISTORE pc
GOTO exec_loop
done:
LDC_W OBJREF
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,13 @@
.constant
port 5555
.end-constant
.main
LDC_W port
NETBIND
NETIN
NETIN
NETOUT
NETOUT
NETCLOSE
.end-main

Binary file not shown.

View File

@@ -0,0 +1,15 @@
.constant
host 2130706433 // 127.0.0.1 -> 0x7F000001
port 5555
.end-constant
.main
LDC_W port
LDC_W host
NETCONNECT
NETIN
NETIN
NETOUT
NETOUT
NETCLOSE
.end-main

BIN
files/fireworks.ijvm Normal file

Binary file not shown.

17330
files/fireworks.jas Normal file

File diff suppressed because it is too large Load Diff

BIN
files/task1/program1.ijvm Normal file

Binary file not shown.

10
files/task1/program1.jas Normal file
View File

@@ -0,0 +1,10 @@
.constant
.end-constant
.main
BIPUSH 0x30
BIPUSH 0x31
IADD
OUT
HALT
.end-main

BIN
files/task1/program2.ijvm Normal file

Binary file not shown.

18
files/task1/program2.jas Normal file
View File

@@ -0,0 +1,18 @@
.constant
piet 1
koos 2
jan 3
.end-constant
.main
NOP
LDC_W piet
DUP
LDC_W koos
IADD
LDC_W jan
IADD
OUT
NOP
HALT
.end-main

Binary file not shown.

View File

@@ -0,0 +1,4 @@
.main
BIPUSH 42
HALT
.end-main

Binary file not shown.

View File

@@ -0,0 +1,4 @@
.main
BIPUSH -42
HALT
.end-main

BIN
files/task2/TestIAND1.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,9 @@
.main
BIPUSH 11
BIPUSH 13
BIPUSH -25
IAND
IAND
OUT
HALT
.end-main

BIN
files/task2/TestIOR1.ijvm Normal file

Binary file not shown.

9
files/task2/TestIOR1.jas Normal file
View File

@@ -0,0 +1,9 @@
.main
BIPUSH 19
BIPUSH 12
BIPUSH 121
IOR
IOR
OUT
HALT
.end-main

BIN
files/task2/TestIadd1.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
.main
BIPUSH 20
BIPUSH 40
IADD
OUT
HALT
.end-main

BIN
files/task2/TestIadd2.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
.main
BIPUSH -20
BIPUSH -40
IADD
OUT
HALT
.end-main

BIN
files/task2/TestIsub1.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
.main
BIPUSH 10
BIPUSH 20
ISUB
OUT
HALT
.end-main

BIN
files/task2/TestIsub2.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
.main
BIPUSH -10
BIPUSH -20
ISUB
OUT
HALT
.end-main

BIN
files/task2/TestPop1.ijvm Normal file

Binary file not shown.

11
files/task2/TestPop1.jas Normal file
View File

@@ -0,0 +1,11 @@
.main
BIPUSH 10
BIPUSH 20
POP
BIPUSH 42
BIPUSH 8
IADD
POP
OUT
HALT
.end-main

BIN
files/task2/TestSwap1.ijvm Normal file

Binary file not shown.

View File

@@ -0,0 +1,8 @@
.main
BIPUSH 10
BIPUSH 20
SWAP
POP
OUT
HALT
.end-main

BIN
files/task3/GOTO1.ijvm Normal file

Binary file not shown.

16
files/task3/GOTO1.jas Normal file
View File

@@ -0,0 +1,16 @@
.constant
.end-constant
.main
L1:
BIPUSH 0x31
OUT
GOTO L3
L2:
BIPUSH 0x32
OUT
L3:
BIPUSH 0x33
OUT
HALT
.end-main

BIN
files/task3/GOTO2.ijvm Normal file

Binary file not shown.

17
files/task3/GOTO2.jas Normal file
View File

@@ -0,0 +1,17 @@
.constant
.end-constant
.main
L1:
BIPUSH 0x31
OUT
GOTO L3
L2:
BIPUSH 0x32
OUT
HALT
L3:
BIPUSH 0x33
OUT
GOTO L2
.end-main

BIN
files/task3/IFEQ1.ijvm Normal file

Binary file not shown.

27
files/task3/IFEQ1.jas Normal file
View File

@@ -0,0 +1,27 @@
.constant
.end-constant
.main
L1:
BIPUSH 0x05
L2:
DUP
BIPUSH 0x30
IADD
OUT
BIPUSH 0x01
ISUB
DUP
IFEQ L3
GOTO L2
L3:
BIPUSH 0x65
BIPUSH 0x6E
BIPUSH 0x6F
BIPUSH 0x64
OUT
OUT
OUT
OUT
.end-main

BIN
files/task3/IFICMPEQ1.ijvm Normal file

Binary file not shown.

31
files/task3/IFICMPEQ1.jas Normal file
View File

@@ -0,0 +1,31 @@
.constant
.end-constant
.main
L1:
BIPUSH 0x00
BIPUSH 0x01
IF_ICMPEQ L5
L2:
BIPUSH 0x00
BIPUSH 0x01
ISUB
BIPUSH 0x01
IF_ICMPEQ L5
L3:
BIPUSH 0x00
BIPUSH 0x01
BIPUSH 0x02
BIPUSH 0x03
L4:
BIPUSH 0x00
IF_ICMPEQ L6
GOTO L4
L5:
HALT
L6:
BIPUSH 0x13
HALT
.end-main

BIN
files/task3/IFLT1.ijvm Normal file

Binary file not shown.

31
files/task3/IFLT1.jas Normal file
View File

@@ -0,0 +1,31 @@
.constant
.end-constant
.main
L1:
BIPUSH 0x00
IFLT L6
L2:
BIPUSH 0x01
IFLT L6
L3:
BIPUSH 0x02
IFLT L6
L4:
BIPUSH 0x10
BIPUSH 0x11
ISUB
IFLT L6
L5:
BIPUSH 0x00
BIPUSH 0x00
BIPUSH 0x00
BIPUSH 0x00
BIPUSH 0x00
HALT
L6:
BIPUSH 0x37
HALT
.end-main

BIN
files/task4/IINCTest.ijvm Normal file

Binary file not shown.

19
files/task4/IINCTest.jas Normal file
View File

@@ -0,0 +1,19 @@
.main
.var
hi
there
.end-var
L1:
BIPUSH 0
DUP
ISTORE hi
ISTORE there
IINC hi 0
IINC hi 1
IINC hi 3
IINC there 0
IINC there -1
IINC there -3
HALT
.end-main

BIN
files/task4/LoadTest1.ijvm Normal file

Binary file not shown.

12
files/task4/LoadTest1.jas Normal file
View File

@@ -0,0 +1,12 @@
.constant
henk 1
piet 2
kees 3
.end-constant
.main
LDC_W henk
LDC_W piet
LDC_W kees
HALT
.end-main

BIN
files/task4/LoadTest2.ijvm Normal file

Binary file not shown.

34
files/task4/LoadTest2.jas Normal file
View File

@@ -0,0 +1,34 @@
.constant
henk 2
piet 3
kees 2
.end-constant
.main
.var
pew
.end-var
BIPUSH 42
LDC_W henk
LDC_W kees
ISUB
IFEQ good
GOTO err
good: LDC_W piet
ISTORE pew
BIPUSH 0
ILOAD pew
BIPUSH 79
OUT
BIPUSH 75
OUT
HALT
err: BIPUSH 69
OUT
BIPUSH 82
OUT
BIPUSH 82
OUT
HALT
.end-main

BIN
files/task4/LoadTest3.ijvm Normal file

Binary file not shown.

30
files/task4/LoadTest3.jas Normal file
View File

@@ -0,0 +1,30 @@
.constant
henk 1
piet 2
kees 3
.end-constant
.main
.var
var1
var2
var3
.end-var
BIPUSH 42
LDC_W henk
ISTORE var1
LDC_W piet
ISTORE var2
LDC_W kees
ISTORE var3
ILOAD var3
ILOAD var2
ILOAD var1
POP
POP
POP
BIPUSH 42
HALT
.end-main

BIN
files/task4/LoadTest4.ijvm Normal file

Binary file not shown.

43
files/task4/LoadTest4.jas Normal file
View File

@@ -0,0 +1,43 @@
.main
.var
i
a
b
c
d
e
f
g
.end-var
BIPUSH 10
ISTORE i
L1: ILOAD i
DUP
BIPUSH 97
IADD
OUT
IFEQ END
BIPUSH 97
ISTORE a
BIPUSH 98
ISTORE b
BIPUSH 99
ISTORE c
BIPUSH 100
ISTORE d
ILOAD i
BIPUSH 1
ISUB
ISTORE i
GOTO L1
END: ILOAD d
ILOAD c
ILOAD b
ILOAD a
OUT
OUT
OUT
OUT
HALT
.end-main

Binary file not shown.

View File

@@ -0,0 +1,13 @@
.main
BIPUSH 0x40
BIPUSH 0x41
BIPUSH 0x42
INVOKEVIRTUAL test
HALT
.end-main
.method test(a, b)
ILOAD a
ILOAD b
IADD
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,11 @@
.main
BIPUSH 0x42
BIPUSH 0x42
INVOKEVIRTUAL test
HALT
.end-main
.method test()
BIPUSH 0x43
BIPUSH 0x43
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,96 @@
// Massive test containing all normal instructions.
//
// NOTE:
// - if ERR is being executed, something is wrong.
// - the stack is the state AFTER execution
.constant
OBJREF 0xdeaddead
ASCII_A 0x41
.end-constant
.main
.var
i
.end-var
BIPUSH 0x10 // stack [16]
DUP // stack [16, 16]
GOTO skipErr1 // skip a byte
ERR
skipErr1:
IADD // stack [32]
BIPUSH 33 // stack [33, 32]
IAND // stack [32]
BIPUSH 0x1 // stack [1, 32]
BIPUSH 0x2 // stack [2, 1, 32]
IOR // stack [3, 32]
ISUB // stack [29]
NOP // stack [29]
BIPUSH 68 // stack [68, 29]
IADD // stack [97]
OUT // stack []
BIPUSH 0 // stack [0]
IFEQ skipErr2 // stack []
ERR
skipErr2:
BIPUSH -22 // stack [-22]
IFLT skipErr3 // stack []
ERR
skipErr3:
LDC_W ASCII_A // stack [65]
BIPUSH 0x41 // stack [65, 65]
IF_ICMPEQ skipErr4 // stack []
ERR
skipErr4:
BIPUSH 0x1 // stack [1]
ISTORE i // stack [], i = 1
ILOAD i // stack [1]
POP // stack []
IINC i 1 // stack [], i = 2
ILOAD i // stack [2]
POP
IN // stack [65] get's actual input 'A'
IN // stack [0, 65] get's EOF
IFEQ skipErr5 // stack [65]
ERR
skipErr5:
BIPUSH 0 // stack [0, 65]
BIPUSH 1 // stack [1, 0, 65]
BIPUSH 2 // stack [2, 1, 0, 65]
BIPUSH 3 // stack [3, 2, 1, 0, 65]
INVOKEVIRTUAL func1 // stack [10, 65]
POP // stack [65]
HALT // stack [65]
.end-main
.method func1(a,b,c)
ILOAD a // stack [1]
POP // stack []
ILOAD b // stack [2]
POP // stack []
ILOAD c // stack [3]
POP // stack []
BIPUSH 0 // stack [0]
BIPUSH 5 // stack [5, 0]
INVOKEVIRTUAL func2 // stack [10]
NOP // stack [10]
IRETURN
.end-method
.method func2(a)
BIPUSH 10 // stack [10]
BIPUSH 0 // stack [0, 10]
BIPUSH 1 // stack [1, 0, 10]
ILOAD a // stack [5, 1, 0, 10]
INVOKEVIRTUAL func3 // stack [5, 10]
POP // stack [10]
IRETURN
.end-method
.method func3(b, a)
ILOAD a // stack [5]
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,14 @@
.main
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 0x0
INVOKEVIRTUAL test
BIPUSH 0x2
HALT
.end-main
.method test()
BIPUSH 0x1
IRETURN
.end-method

Binary file not shown.

View File

@@ -0,0 +1,18 @@
.main
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 0x0
BIPUSH 0x2
BIPUSH 0x3
INVOKEVIRTUAL add
BIPUSH 0x2
HALT
.end-main
.method add(a, b)
ILOAD a
ILOAD b
IADD
IRETURN
.end-method

BIN
files/testinvoke.ijvm Normal file

Binary file not shown.