Home > database >  How to display the value of a register or variable | TASM
How to display the value of a register or variable | TASM

Time:10-09

I have a task: to count the number of elements in the array that are less than the entered number. How to properly compare an array element with this variable? It turns out to compare the value of an element with a constant, but not with the entered number My code:

.model small
.stack 100h
.data
array db 1, 2, 3, 4, 7, 5, 6
n db 3, ?, 3 dup(?) 
counter dw 0
.code
StrToNumber PROC 
    mov bh, [si   2]
    mov bl, [si   3]
    sub bx, 3030h
    mov al, 10
    mul bh
    add al, bl 
    ret
ENDP
start: 
;started program exe
mov ax,@data
mov ds, ax


mov ah, 0Ah
lea dx, n
int 21h


lea si, n
call StrToNumber
mov cl, al 


lea bx, array
beg:
mov ax, [bx]
cmp ax, 0
jge skip
push bx
lea bx, counter
mov ax, [bx]
inc ax
mov [bx], ax
pop bx

skip:
    add bx, 4
    loop beg




exit:
mov ax,4c00h
int 21h
end start

CodePudding user response:

Your StrToNumber proc is rather special, but it will yield correct results if you input single digit values with a prepended 0. Like "04" for the value 4.

This is wrong

The loop beg instruction depends on the CX register. You have setup the CL register only. You need to zero CH also.

The array is defined to contain bytes, yet your code processes words and advances by dwords! Make everything byte-sized.

This is too complicated

The code:

push bx
lea bx, counter
mov ax, [bx]
inc ax
mov [bx], ax
pop bx

is just inc counter

This is a solution

Below code leaves the inputted number in AL, so it can use CX to count the number of iterations for the loop.

  call StrToNumber ; -> AL is the inputted number e.g. 04

  mov cx, 7        ; 7 elements in the array
  lea bx, array
beg:
  cmp [bx], al
  jnl skip         ; Skip if element is not less than inputted value
  inc counter      ; Count elements that are less than inputted value
skip:
  inc bx           ; BYTE sized elements
  dec cx
  jnz beg

If ever you need to display some number on the screen, I have prepared a nice explanation (with code) in this Q/A Displaying numbers with DOS.

  • Related