0

I want to store random numbers from 1 to 5 in an array of 100 bytes. My random procedure is working well but it did not show any output when I tried to print my array. This program did not show any output. I have checked my random number procedure and it works fine, but I think there is some issue with storing and printing.
Here is my code:

    .model small
    .stack 100h
    .data
    arrayelement db 100 dup('$'); To store array
    randomnumber db 0; to store random number
    r1 db ?
    .code
    main proc
        mov ax,@data
        mov ds,ax
  
        mov cx,100
        mov si,offset arrayelement
        forr:
    
            call random
            mov al,randomnumber
            mov [si],al
            inc si
            
        loop forr
    
        mov dl,10
        mov ah,02h
        int 21H
        mov dl,13
        mov ah,02h
        int 21H
    
        mov si,offset arrayelement
        mov cx,100
        for1:
        mov dl,[si]
        ;add dl,48
        mov ah,02
        int 21h
        inc si
        loop for1
        
        mov ah,4ch
        int 21h
    main endp
;Random procedure to print randomnumber between 1 to 5
    random proc
        mov ax,0
        mov bx,0
        mov cx,0
        mov dx,0
;Loop to slow down time so it print different random number on each call
      mov cx,500
      l11:
        push cx
        mov cx,500
      l222:
        Loop l222
        pop cx
        Loop l11
      MOV AH, 00h  ; interrupts to get system time        
       INT 1AH      ; CX:DX now hold number of clock ticks since midnight      
    
       mov  ax, dx
       xor  dx, dx
       mov  cx, 5 ;Ending  
     
       div  cx       ; here dx contains the remainder of the division - from 1-5
    
       add  dl, 1  ; start--to ascii from '1' to '5'
       mov randomnumber,dl
      
      ret
      random endp
    end main
Sep Roland
  • 26,423
  • 5
  • 40
  • 66
Muhammad
  • 29
  • 5
  • 2
    Use a debugger to single-step your code: when `random` returns, CX is always 5, so `loop` is always taken. Use a register for you loop counter which your function doesn't destroy. Also, you could use any PRNG algorithm that you seed once, instead of using a delay loop(!) to give the clock time to tick. And you could just return the random number in AL like a normal function, or even in DL where it already is. You generally don't need to use global variables. – Peter Cordes Jun 11 '21 at 07:44
  • 1
    Duplicate of [TASM infinite loop](https://stackoverflow.com/a/37810356) - same problem of a function call clobbering the loop counter, but nobody's upvoted my answer there. I'm sure there must be other duplicates, but having a hard time finding them. – Peter Cordes Jun 11 '21 at 08:03

1 Answers1

-1
.model small
.stack 100h
.data
arrayelement db 100 dup('$'); To store name
randomnumber db 0; to store random number
r1 db ?
.code
main proc
    mov ax,@data
    mov ds,ax
    
    mov cx,100
    mov si,offset arrayelement
    forr:

        call random
        mov al,randomnumber
        mov [si],al
        inc si
        
    loop forr

    mov dl,10
    mov ah,02h
    int 21H
    mov dl,13
    mov ah,02h
    int 21H

    mov si,offset arrayelement
    mov cx,100
    for1:
    mov dl,[si]
    add dl,48
    mov ah,02
    int 21h
    inc si
    loop for1
    
    mov ah,4ch
    int 21h
main endp
random proc
    push cx
    push ax
    push dx
  mov cx,500
  l11:
    push cx
    mov cx,500
  l222:
    Loop l222
    pop cx
    Loop l11
  MOV AH, 00h  ; interrupts to get system time        
   INT 1AH      ; CX:DX now hold number of clock ticks since midnight      

   mov  ax, dx
   xor  dx, dx
   mov  cx, 5 ;Ending  
 
   div  cx       ; here dx contains the remainder of the division - from 1-5

   add  dl, 1  ; start--to ascii from '1' to '5'
   mov randomnumber,dl
  pop cx
    pop ax
    pop dx
  ret

  random endp
end main
Muhammad
  • 29
  • 5
  • 2
    I see that you have tried to correct your program following the comment provided by @PeterCordes. It's OK to preserve the registers involved on the stack **but you must restore those registers in reverse order** because that is how the stack works. It's Last In, First Out. So `push cx` `push ax` `push dx` must ultimately lead to `pop dx` `pop ax` `pop cx` `ret` – Sep Roland Jun 12 '21 at 20:46
  • 2
    In order to turn this answer into something we can upvote (I will gladly do so), you need to explain in your own words how the problem in the original question was resolved. Answers that only contain code and nothing else, are not generally recognised as good answers! – Sep Roland Jun 12 '21 at 20:49