-
Notifications
You must be signed in to change notification settings - Fork 1
/
Walk.asm
executable file
·103 lines (87 loc) · 2.09 KB
/
Walk.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
TITLE Drunkard's Walk (Walk.asm)
; Drunkard's walk program. This is a simple version that
; starts at coordinate 50,50 and wanders around the immediate area.
; Line 62: inc currY
; Line 65: dec currY
INCLUDE Irvine32.inc
WalkMax = 30
StartX = 25
StartY = 25
DrunkardWalk STRUCT
path COORD WalkMax DUP(<0,0>)
pathsUsed WORD 0
DrunkardWalk ENDS
DisplayPosition PROTO currX:WORD, currY:WORD
.data
aWalk DrunkardWalk <>
.code
main PROC
mov esi,offset aWalk
call TakeDrunkenWalk
exit
main ENDP
;-------------------------------------------------------
TakeDrunkenWalk PROC
LOCAL currX:WORD, currY:WORD
;
; Take a walk in random directions (north, south, east,
; west).
; Receives: ESI points to a DrunkardWalk structure
; Returns: the structure is initialized with random values
;-------------------------------------------------------
pushad
; Point EDI to the array of COORD objects.
mov edi,esi
add edi,OFFSET DrunkardWalk.path
mov ecx,WalkMax ; loop counter
mov currX,StartX ; current X-location
mov currY,StartY ; current Y-location
Again:
; Insert current location in array.
mov ax,currX
mov (COORD PTR [edi]).X,ax
mov ax,currY
mov (COORD PTR [edi]).Y,ax
INVOKE DisplayPosition, currX, currY
mov eax,4 ; choose a direction (0-3)
call RandomRange
.IF eax == 0 ; North
inc currY
.ELSEIF eax == 1 ; South
dec currY
.ELSEIF eax == 2 ; West
dec currX
.ELSE ; East (EAX = 3)
inc currX
.ENDIF
next:
add edi,TYPE COORD ; point to next COORD
loop Again
finish:
mov ax,WalkMax ; count the steps taken
sub ax,cx
mov (DrunkardWalk PTR [esi]).pathsUsed, ax
popad
ret
TakeDrunkenWalk ENDP
;-------------------------------------------------------
DisplayPosition PROC currX:WORD, currY:WORD
;
; Display the current X and Y positions.
; Optional: used for debugging.
;-------------------------------------------------------
.data
commaStr BYTE ",",0
.code
pushad
movzx eax,currX ; current X position
call WriteDec
mov edx,OFFSET commaStr ; "," string
call WriteString
movzx eax,currY ; current Y position
call WriteDec
call Crlf
popad
ret
DisplayPosition ENDP
END main