Skip to content

Commit 307f467

Browse files
committed
fix(tic-tac-toe): fix constructor in masm contract
1 parent 3ac342a commit 307f467

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

masm/accounts/tic_tac_toe.masm

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,60 @@ const.MAPPING_SLOT=3
2828

2929
# => [player1_prefix, player1_suffix, player2_prefix, player2_suffix]
3030
export.constructor
31-
debug.stack
32-
# store player1 ID
33-
push.PLAYER1_SLOT debug.stack exec.account::set_item
34-
# => [player2_prefix, player2_suffix]
31+
# store player1 ID by padding value to size of one word
32+
push.0.0 push.PLAYER1_SLOT
33+
# [player1_slot, 0, 0, player1_prefix, player1_suffix, player2_prefix, player2_suffix]
34+
35+
exec.account::set_item
36+
# [OLD_VALUE, player2_prefix, player2_suffix]
37+
38+
# drop old value from stack
39+
dropw
40+
# [player2_prefix, player2_suffix]
3541

3642
# pad to word
43+
push.0.0 push.PLAYER2_SLOT
44+
# [player2_slot, 0, 0, player2_prefix, player2_suffix]
3745

3846
# store player2 ID
39-
push.PLAYER2_SLOT exec.account::set_item
40-
# => []
47+
exec.account::set_item
48+
49+
# Drop old value from stack (returned by set_teim call)
50+
dropw
51+
# []
4152
end
4253

43-
# => [caller_suffix, caller_prefix, field_index]
54+
# => [caller_prefix, caller_suffix, field_index]
4455
export.make_a_move
4556
debug.stack
46-
# Stack: [caller_suffix, caller_prefix, field_index]
57+
# Stack: [caller_prefix, caller_suffix, field_index]
4758

4859
# verify caller ID is in line with current player
4960
push.FLAG_SLOT exec.account::get_item
50-
# [flag, caller_suffix, caller_prefix, field_index]
61+
# [flag, caller_prefix, caller_suffix, field_index]
5162

5263
# Push zero (to compare to zero)
5364
padw
54-
# [0 word, FLAG, caller_suffix, caller_prefix, field_index]
65+
# [0WORD, FLAG, caller_prefix, caller_suffix, field_index]
5566

5667
# Check if equal
5768
eqw
58-
# [is_true, caller_suffix, caller_prefix, field_index]
69+
# [is_true, caller_prefix, caller_suffix, field_index]
5970

6071
if.true
61-
push.PLAYER1_SLOT push.1
62-
# [player1_slot, 1, caller_suffix, caller_prefix, field_index]
63-
swap.4
64-
# [field_index, player1_slot, 1, caller_suffix, caller_prefix]
65-
# [player1_slot, caller_suffix, caller_prefix, field_index, 1]
72+
push.PLAYER1_SLOT push.1
73+
# [1, player1_slot, caller_prefix, caller_suffix, field_index]
74+
movdn.4
75+
# [player1_slot, caller_prefix, caller_suffix, field_index, 1]
6676
else
67-
push.PLAYER2_SLOT push.2 swap.4
68-
# [player2_slot, caller_suffix, caller_prefix, field_index, 2]
77+
push.PLAYER2_SLOT push.2
78+
# [2, player2_slot, caller_prefix, caller_suffix, field_index]
79+
movdn.4
80+
# [player2_slot, caller_prefix, caller_suffix, field_index, 2]
6981
end
7082

7183
exec.account::get_item
72-
# [player2_suffix, player2_prefix, caller_suffix, caller_prefix, field_index, move_value]
84+
# [player_prefix, player_suffix, caller_prefix, caller_suffix, field_index, move_value]
7385

7486
exec.account_id::is_equal assert.err=ERR_WRONG_PLAYER
7587
# [field_index, move_value]

0 commit comments

Comments
 (0)