Skip to content

Instantly share code, notes, and snippets.

@Chrispassold
Last active November 28, 2020 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chrispassold/e8e2e97b500b5b4fcf42fc15c2f07cda to your computer and use it in GitHub Desktop.
Save Chrispassold/e8e2e97b500b5b4fcf42fc15c2f07cda to your computer and use it in GitHub Desktop.
; multi-segment executable file template.
#start=robot.exe#
r_port equ 9
n_lamps equ 3
data segment
; add your data here!
n_lamps_on db ?
last_random db ?
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
; add your code here
mov n_lamps_on, 0
mov last_random, 10
eternal_loop:
cmp n_lamps_on, n_lamps
je finish
; examine the area
; in front of the robot:
mov al, 4
out r_port, al
call wait_exam
; nothing found?
cmp al, 0
je mover ; - yes, so continue.
; wall?
cmp al, 255
je mover ; - yes, so continue.
; is lamp off?
cmp al, 8
je lamp_switch_on
jmp mover ; continue
lamp_switch_on:
call switch_on_lamp
inc n_lamps_on
jmp mover ; continue
mover:
call random_turn
call wait_robot
; try to step forward:
mov al, 1
out r_port, al
call wait_robot
jmp eternal_loop ; go again!
finish:
call wait_robot
jmp finish
;===================================
; this procedure does not
; return until robot is ready
; to receive next command:
wait_robot proc
; check if robot busy:
busy: in al, r_port+2
test al, 00000010b
jnz busy ; busy, so wait.
ret
wait_robot endp
;===================================
; this procedure does not
; return until robot completes
; the examination:
wait_exam proc
mov cx,50
pausa:
loop pausa
; check if has new data:
busy2: in al, r_port+2
test al, 00000001b
jz busy2 ; no new data, so wait.
in al, r_port+1
ret
wait_exam endp
;===================================
;===================================
; switch off the lamp:
switch_off_lamp proc
mov al, 6
out r_port, al
ret
switch_off_lamp endp
;===================================
; switch on the lamp:
switch_on_lamp proc
mov al, 5
out r_port, al
ret
switch_on_lamp endp
;===================================
; generates a random turn using
; system timer:
random_turn proc
; get number of clock
; ticks since midnight
; in cx:dx
mov ah, 0
int 1ah
; randomize using xor:
xor dh, dl
xor ch, cl
xor ch, dh
cmp ch, last_random
je random_turn
mov last_random, ch
test ch, 2
jz no_turn
test ch, 1
jnz turn_right
; turn left:
mov al, 2
out r_port, al
; exit from procedure:
ret
turn_right:
mov al, 3
out r_port, al
no_turn:
ret
random_turn endp
ends
end start ; set entry point and stop the assembler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment