2

Could someone please explain to me why the following program won't show anything on screen? So what I tried to do is calculate the sum of a vector like so:

.model small
.stack 100h
.data
  vector db  1,2,3,4,5,6,7,8,9
  suma db 0
  count db 9
  msg db 10,13,"Sum is:$"

.code
  mov ax,@data
  mov ds,ax

  mov si,0
  xor si,si
  xor cx,cx
  mov cl,count
repeta:
  mov al,vector[si]
  add suma,al
  inc si
loop repeta

  mov bx,ax
  mov ah,09
  lea dx,msg
  int 21h
  mov ah,2
  mov dl,bl
  int 21h
  mov ah,2
  mov dl,bl
  int 21h

  mov ah,4ch
  int 21h    
end
zx485
  • 28,498
  • 28
  • 50
  • 59
Lola
  • 218
  • 1
  • 11
  • That's just an array, not a SIMD vector ([Fastest way to do horizontal float vector sum on x86](https://stackoverflow.com/q/6996764)) or a C++-style `std::vector` (dynamically allocated and growable) http://en.cppreference.com/w/cpp/container/vector. In C, what you have would be `uint8_t vector[] = {1,2,3,4,5,6,7,8,9};`, i.e. an array with a weird name. – Peter Cordes May 28 '18 at 01:25
  • 1
    There's no reason for your `suma` variable to be in memory. Add into a register; that's what they're for. e.g. `add al, vector[si]` to get your result in `al`. The name for AL / AX comes from "accumulator", which is literally what you're doing here. – Peter Cordes May 28 '18 at 01:28

1 Answers1

7
mov bx,ax

There's nothing meaningful in AX at this point in the program. The value that you're after is in the suma variable.

Next code will show results provided the sum stays below 100.

mov ah, 09h
lea dx, msg
int 21h

mov al, suma     ;Result happens to be 55
aam              ;Tens go to AH, units go to AL
or  ax, "00"     ;Make ASCII characters
mov bx, ax       ;Move to safe place

mov ah, 02h
mov dl, BH       ;Display tens
int 21h
mov ah, 02h
mov dl, BL       ;Display units
int 21h

mov ah, 00h
int 16h          ;Wait for a key
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 1
    It still doesn't output. Should I make any other change? – Lola May 27 '18 at 19:30
  • 1
    @Lola Perhaps the output doesn't stay on screen long enough for you to see it. Try waiting for a key. I'll add the code to my answer. – Sep Roland May 27 '18 at 19:35