1
votes

say i have to generate 20 random coordinates, 20 row and 20 column numbers.

.model small
.data
;///////////////////// VARIABLES
;array of colors
color db 01h,02h,03h,04h,05h,06h,0Ah, 0Bh, 0Ch,0Dh,0Eh, 0Fh ;12 colors.

star_char db '*', '$'

;coordinates for star.
;center coordinates 
row_star db 20 dup(?)
col_star db 20 dup(?)

gen_row_star db ?
gen_col_star db ?
;star coordinates

go_up db 20 dup(?)
go_down db 20 dup(?)
go_right db 20 dup(?)
go_left db 20 dup(?)

; create temporary storage variables
temp_row_star db 20 dup(?)
temp_col_star db 20 dup(?)


temp_go_up db 20 dup(?)
temp_go_down db 20 dup(?)
temp_go_right db 20 dup(?)
temp_go_left db 20 dup(?)

;boom coordinates
delaytime db ?
delay_star db 10
delay_printing db 3
.stack 100h

total_blink db 5
total_print db 20
;///// Main code:
.code

delay proc
    ;randomize time 5-15secs
    mov ah, 00h
    int 1ah

    mov ax,dx
    xor dx,dx
    mov cx, 10
    div cx
    add dl, 15
    mov delaytime, dl

    mov ah, 00
    int 1Ah
    mov bx, dx

jmp_delay:
    int 1Ah
    sub dx, bx
    cmp dl, delaytime
    jl jmp_delay

    ret
delay endp



random_coor_star proc

mov ah, 00h
int 1ah

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,10
div cx

add dl, 5
mov al,dl
mov gen_row_star,al

mov ah, 00h
int 1ah

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,50
div cx
add dl,5
mov al,dl
mov gen_col_star,al

random_coor_star endp





clear proc near
;set video mode + clear screen + stop cursor blinking
    mov al, 03h 
    mov ah, 00h
    int 10h

    mov cx, 3200h  ;stop cursor blinking
    mov ah, 01h
    int 10h
    ret

clear endp

complete_print macro r,c,char,color
mov dh, r
mov dl, c
xor bh,bh
mov ah,02h
int 10h

    mov al, char
    mov bh, 0
    mov bl, color
    mov cx, 1
    mov ah, 09h
    int 10h 

endm

main proc

mov ax, @data
mov ds, ax

mov al, 03h 
    mov ah, 00h
    int 10h

;generate 20 numbers and store in each variable.
mov bl,0
transfer:
call random_coor_star
mov dl, gen_row_star
mov [row_star+bx],dl

mov dl, gen_col_star
mov [col_star+bx],dl
call delay
cmp bx,20
jl transfer


mov ax, 4c00h
int 21h

main endp
end main

is there a way i can display all of the numbers i stored in the array? tried using offset but didnt work.

1
Write a function that prints an integer (by first converting it to a string), and then call the function for each value in the array.Michael

1 Answers

2
votes

Next is a little program to display an array of numbers (EMU8086 Intel sytanx) :

.stack 100h
.data

numbers dw 130,50,260,570,180,190,100,20,66,140,860,470,190,20,220,250,170,30,300,0

str  db 6 dup('$') ;STRING TO STORE NUMBER CONVERTED INTO STRING.
crlf db 13,10,'$'

.code          
;INITIALIZE DATA SEGMENT.
  mov  ax, @data
  mov  ds, ax

  mov  di, offset numbers  ;DI POINTS TO THE NUMBERS.
while:
;CONVERT CURRENT NUMBER TO STRING.
  call dollars  ;FILL "STR" WITH '$'. NECESSARY TO DISPLAY IT.
  mov  ax, [ di ]  ;GET CURRENT NUMBER.
  call number2string  ;CONVERT AX TO STRING. RESULT IN "STR".  
;DISPLAY NUMBER CONVERTED.  
  mov  ah, 9
  mov  dx, offset str
  int  21h        
;DISPLAY LINE BREAK.
  mov  ah, 9
  mov  dx, offset crlf
  int  21h        
;NEXT NUMBER TO PROCESS.  
  add  di, 2  ;2 BYTES FORWARD BECAUSE NUMBERS ARE "DW".
;CHECK IF PROCESS IS OVER.  
  mov  ax, [ di ]
  cmp  ax, 0
  jne  while  ;IF AX!=0 JMP WHILE.

;FINISH PROGRAM.
  mov  ax, 4c00h
  int  21h           

;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING.

proc number2string
  mov  bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
  mov  cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:       
  mov  dx, 0 ;NECESSARY TO DIVIDE BY BX.
  div  bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
  push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
  inc  cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
  cmp  ax, 0  ;IF NUMBER IS
  jne  cycle1 ;NOT ZERO, LOOP. 
;NOW RETRIEVE PUSHED DIGITS.
  mov  si, offset str
cycle2:  
  pop  dx        
  add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
  mov  [ si ], dl
  inc  si
  loop cycle2  

  ret
endp  

;------------------------------------------
;FILLS VARIABLE STR WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THESE STRINGS WILL BE DISPLAYED.

proc dollars                 
  mov  si, offset str
  mov  cx, 6
six_dollars:      
  mov  al, '$'
  mov  [ si ], al
  inc  si
  loop six_dollars
  ret
endp

Now previous code with your random proc, it first fills the array with 40 random numbers (20 pairs X,Y), and displays them :

.stack 100h
.data            

gen_row_star db ?
gen_col_star db ?

array db 40 dup(?)  ;STORES 20 PAIRS OF RANDOM NUMBERS.

str   db 6 dup('$') ;STRING TO STORE NUMBER CONVERTED INTO STRING.
comma db ',$'
crlf  db 13,10,'$'

.code          
;INITIALIZE DATA SEGMENT.
  mov  ax, @data
  mov  ds, ax

;FILL ARRAY WITH RANDOM NUMBERS.
  mov  di, offset array
  mov  bp, 20  ;GENERATE 20 PAIRS OF RANDOM NUMBERS.
fill_array:
  call random_coor_star ;NUMBERS RETURN IN gen_row_star AND gen_col_star.
  mov  al, gen_col_star 
  mov  [ di ], al  ;MOVE FIRST NUMBER OF PAIR TO ARRAY (X COORDINATE).
  inc  di
  mov  al, gen_row_star 
  mov  [ di ], al  ;MOVE SECOND NUMBER OF PAIR TO ARRAY (Y COORDINATE).
  inc  di
  dec  bp
  jnz  fill_array

;DISPLAY ARRAY.  
  mov  di, offset array  ;DI POINTS TO THE ARRAY.
  mov  bp, 20  ;DISPLAY 20 PAIRS OF RANDOM NUMBERS.
while:
;- - - X COORDINATE.
;CONVERT CURRENT NUMBER TO STRING.
  call dollars  ;FILL "STR" WITH '$'. NECESSARY TO DISPLAY IT.
  mov  al, [ di ]  ;GET CURRENT NUMBER.
  mov  ah, 0   ;CLEAR AH TO USE AX.
  call number2string  ;CONVERT AX TO STRING. RESULT IN "STR".  
;DISPLAY NUMBER CONVERTED.  
  mov  ah, 9
  mov  dx, offset str
  int  21h        
;DISPLAY COMMA.
  mov  ah, 9
  mov  dx, offset comma
  int  21h        
;- - - Y COORDINATE.
  inc  di  ;NEXT NUMBER OF CURRENT PAIR.
;CONVERT CURRENT NUMBER TO STRING.
  call dollars  ;FILL "STR" WITH '$'. NECESSARY TO DISPLAY IT.
  mov  al, [ di ]  ;GET CURRENT NUMBER.
  mov  ah, 0   ;CLEAR AH TO USE AX.
  call number2string  ;CONVERT AX TO STRING. RESULT IN "STR".  
;DISPLAY NUMBER CONVERTED.  
  mov  ah, 9
  mov  dx, offset str
  int  21h        
;DISPLAY LINE BREAK.
  mov  ah, 9
  mov  dx, offset crlf
  int  21h        
;CHECK IF PROCESS IS OVER.  
  inc  di  ;NEXT NUMBER (NEXT PAIR OF COORDINATES).
  dec  bp
  jnz  while 

;FINISH PROGRAM.
  mov  ax, 4c00h
  int  21h           

;------------------------------------------
;DR.DOOM'S RANDOM PROC.

random_coor_star proc

mov ah, 00h
int 1ah

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,10
div cx

add dl, 5
mov al,dl
mov gen_row_star,al

mov ah, 00h
int 1ah

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,50
div cx
add dl,5
mov al,dl
mov gen_col_star,al

ret

random_coor_star endp  

;------------------------------------------
;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING.

proc number2string
  mov  bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
  mov  cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:       
  mov  dx, 0 ;NECESSARY TO DIVIDE BY BX.
  div  bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
  push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
  inc  cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
  cmp  ax, 0  ;IF NUMBER IS
  jne  cycle1 ;NOT ZERO, LOOP. 
;NOW RETRIEVE PUSHED DIGITS.
  mov  si, offset str
cycle2:  
  pop  dx        
  add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
  mov  [ si ], dl
  inc  si
  loop cycle2  

  ret
endp  

;------------------------------------------
;FILLS VARIABLE STR WITH '$'.
;USED BEFORE CONVERT NUMBERS TO STRING, BECAUSE
;THESE STRINGS WILL BE DISPLAYED.

proc dollars                 
  mov  si, offset str
  mov  cx, 6
six_dollars:      
  mov  al, '$'
  mov  [ si ], al
  inc  si
  loop six_dollars
  ret
endp