Sorin
Sorin

Reputation: 910

Computing the number from array's elements in assembly

I read a number from keyboard and stored it in an array called buf. I also have the length of the array in the len variable.

I am trying now to compute the number from that array. My code is this:

   xor si, si
   xor bx, bx ; made them 0 
   start_for:
    cmp si, len
    je end_for
    mul bx, 10 ; I think here is the problem! 
    mov al, buff[si]  
    sub al, '0'
    add bx, ax
    inc si
    jmp start_for

   end_for:

What is the problem?

I noticed on debug that the line 'mul bx, 10' has no effect.

Upvotes: 2

Views: 176

Answers (2)

Sorin
Sorin

Reputation: 910

I was incorrectly using the MUL instruction. Modified the program and it works:

xor si, si
xor ax, ax        

   start_for:
    cmp si, len
    je end_for 
    mov bx, 10
    mul bx ; This means AX = AX * BX (for 8 bit operands)
    mov bh, 0 
    mov bl, buff[si]  
    sub bl, '0'
    add ax, bx
    inc si
    jmp start_for

   end_for:  

AX and BX registers changed their meaning. AX will store the number and BX is used only for MUL.

Upvotes: 1

chux
chux

Reputation: 154562

In the line add bx, ax, it appears that the upper byte ah is not yet defined. I recommend setting ax or ah to 0.

Also, i86 might not put the 'mul' product where you think. The product may be in DX:AX even with a BX operand. Recommend swapping use of AX and BX in your code. Let AX be your final product and bx your single digit value.

Upvotes: 3

Related Questions