A
A
Alertoso2021-04-01 23:34:36
assembler
Alertoso, 2021-04-01 23:34:36

Why is the cx register not reset?

org 100h

Start:

        mov     ah, 0ah
        mov     dx, firstNum
        int     21h

        mov     ah, 02h
        mov     dx, 10
        int     21h

        mov     bx, firstNum
        call    A1

        mov     ah, $08
        int     21h
        mov     ax, 4c00h               ;выход
        int     21h


A1:
        push    ax
        push    cx
        push    dx
        push    di
        xor     cx, cx

        mov     di, 10
        mov     cx, [bx+2]
        mov     si, 2

.A2:
        xor     dx, dx
        mov     dx, [bx+si]
        sub     dx, '0'
        add     ax, dx
        mul     di
        inc     si
        loop    .A2

        pop     si
        pop     di

        call    Show_AX

Show_AX:
        push    ax
        push    cx
        push    dx
        push    di

        mov     di, 10                  ;di - основание с/c(системы счисления)
        xor     cx, cx                  ;cx - количество цифр в числе
 
@@Conv:
        xor     dx, dx
        div     di                      ;dl = num mod 10
        add     dl, '0'                 ;перевод в символьный формат
        inc     cx
        push    dx                      ;складываем в стeк
        or      ax, ax
        jnz     @@Conv

;вывод из стека на экран

@@Show:
        pop     dx                      ;dl = очередной символ
        mov     ah, $02                 ;ah - функция вывода символа на экран
        int     21h

        loop    @@Show
 
        pop     di
        pop     dx
        pop     cx
        pop     ax

        mov     ah, $08
        int     21h
        mov     ax, 4c00h               ;выход
        int     21h

firstNum        dw 6,0,5 dup(?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-04-02
@jcmvbkbc

Why is the cx register not reset?

It seems to me that you want to ask a completely different question. For example, "why subroutine A1 of translating a string into its numeric value does not work correctly." The answer to this question is because there are two errors in the following code:
A1:
        push    ax
        push    cx
        push    dx
        push    di
        xor     cx, cx

        mov     di, 10
        mov     cx, [bx+2]

- firstly, the length of the string is written at offset 1 in the buffer, which is filled by interrupt function 0ah 21h ,
- secondly, the length of the string is represented by one byte, not a word, as you read.
With that in mind, the code should look like this:
A1:
        push    ax
        push    cx
        push    dx
        push    di
        xor     cx, cx

        mov     di, 10
        mov     cl, [bx+1]

And the buffer for inputting a string should be defined like this:
firstNum db 6,0,6 dup(?)
Next, here again you confuse bytes with words and load more than necessary into dx:
.A2:
        xor     dx, dx
        mov     dx, [bx+si]
        sub     dx, '0'
        add     ax, dx

should be
.A2:
        xor     dx, dx
        mov     dl, [bx+si]
        sub     dl, '0'
        add     ax, dx

Then you have some nonsense with the stack balance:
A1:
        push    ax
        push    cx
        push    dx
        push    di
...
        pop     si
        pop     di

you put one thing in it, and you take off another and in a different quantity. But it doesn't matter, because you don't return anywhere from function A1 anyway. Show_AX looks ok.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question