domingo, 12 de outubro de 2025

emu8086 - count

/* Main.c file generated by New Project wizard

 *

 * Created:   sáb out 11 2025

 * Processor: 8086

 * Compiler:  Digital Mars C

 *

 * Before starting simulation set Internal Memory Size 

 * in the 8086 model properties to 0x10000

 */



// robot base i/o port:

#define r_port 9


unsigned int count = 0;


void Robot(void)

{

   asm {


//===================================


mov ax,0

mov dx,199

out dx,ax


eternal_loop:

      

cmp count,5

je Happy      

      

// wait until robot

// is ready:

call wait_robot


// examine the area

// in front of the robot:

mov al, 4

out r_port, al


call wait_exam


// get result from

// data register:

in al, r_port + 1


// nothing found?

cmp al, 0

je cont  // - yes, so continue.


// wall?

cmp al, 255  

je cont  // - yes, so continue.


// switched-on lamp?

cmp al, 7

jne lamp_off  // - no, so skip.

// - yes, so switch it off,

//   and turn:

//call switch_off_lamp 

jmp  cont  // continue


lamp_off: nop


// if gets here, then we have

// switched-off lamp, because

// all other situations checked

// already:

call switch_on_lamp


cont:

call random_turn


call wait_robot


// try to step forward:

mov al, 1

out r_port, al


call wait_robot


// try to step forward again:

mov al, 1

out r_port, al


jmp eternal_loop // go again!


//===================================


// this procedure does not

// return until robot is ready

// to receive next command:

wait_robot:

// check if robot busy:

busy: in al, r_port+2

      test al, 0b00000010

      jnz busy // busy, so wait.

ret    


//===================================


// this procedure does not

// return until robot completes

// the examination:

wait_exam:

// check if has new data:

busy2: in al, r_port+2

       test al, 0b00000001

       jz busy2 // no new data, so wait.

ret    



//===================================


// switch off the lamp:

switch_off_lamp:

mov al, 6

out r_port, al

ret



//===================================


// switch on the lamp:

switch_on_lamp:


syncronize: 

mov al, 5

out r_port, al

// wait until robot

// is ready:

call wait_robot

// examine the area

// in front of the robot:

mov al, 4

out r_port, al


call wait_exam


// get result from

// data register:

in al, r_port + 1


cmp al,8

je syncronize

cmp al,255

je syncronize

          

inc count


mov ax,count

mov dx,199

out dx,ax          


ret




//===================================


// generates a random turn using

// system timer:

random_turn:


// 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


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


//===================================


Happy:

     mov al, 3

     out r_port, al

     call wait_robot

// try to step forward:

mov al, 1

out r_port, al


call wait_robot

     jmp Happy

   }

   

}



void main(void)

 {

   // Write your code here

   Robot();

   while (1)

   {

   }

 }

Nenhum comentário:

Postar um comentário