Changing memory in an assembly application

I am learning assembler from the book "The Art of x86 Assembly" and I have a question that I could not understand the answer to.

the program will look like this:

"in this exercise, you run a program that checks and uses the values ​​found in memory, then you switch to the memory screen and change the values ​​in memory (that is, you will access memory directly while the program continues to run).

The program starts by setting the memory numbering 1000h to zero, then it is executed until one of two conditions is met: either the user toggles the FFF0 switch, or the user changes the value in the memory location 1000h. Switching the switch FFF0 terminates the program.

Changing the value in memory location 1000h transfers control to the program section, which concatenates n words, where n is the new value in memory location 1000h. "

After he sums these values, he prints his sum using "put"

I have this code:

d:  mov cx,0
    mov [1000],cx

a:  mov cx,[1000]
    cmp cx,0
    jne c

    mov ax,[fff0]
    cmp ax,0
    je a
    halt

c:  mov bx,1002
    mov ax,0

b:  add ax,[bx]
    add bx,2
    sub cx,1
    cmp cx,0
    jne b

    put
    jmp d

      

The problem is that when I put 12h to 1000h, the program gives out 2 values, sum and number 1.

When I exit through the program, it outputs 1 value (sum), but when I run it, it outputs 2 values ​​(sum and number 1).

Can someone explain this behavior?

+1


source to share


1 answer


I interpreted the code, I hope it helps someone:

Note. This code does not cover multithreading or register-changing interrupts. This is a straightforward top-down analysis (to help the novice, not the "sage").



d:  mov cx,0 ; cx = 0
    mov [1000],cx ; Memory address 1000=0

a:  mov cx,[1000] ; Get from memory address 1000 to cx (cx=0)
    cmp cx,0 ; Is cx = 0?
    jne c ; If cx not 0 goto c

    mov ax,[fff0] ; ax = {whatever is at FFF0}
    cmp ax,0 ; Is ax=0?
    je a ; If ax = 0 goto a
    halt ; {STOP PROCESSOR} 

c:  mov bx,1002 ;  BX = 1002
    mov ax,0 ; AX = 0

b:  add ax,[bx] ; Add to ax WHATEVER is in MEMORY location bx
    add bx,2 ; Add 2 to bx
    sub cx,1 ; Deduct 1 from cx
    cmp cx,0 ; is cx = 0?
    jne b ; If cx is not 0 goto b

    put {what do you want to print?}  
    jmp d ; goto d (back to the top)

      

0


source







All Articles