tags:

views:

157

answers:

1

Hey I'm studying assembly language from the book "art of x86 assembly" and I have a question I could'nt figure the answer.

the program goes like this: "in this exercise you will start a program running that examines and operates on values found in mmory. then you will switch to the memory screen and modify values in memory (that is, you will directly access memory while the program continues torun). th program begins by seting mmory location 1000h to zero, then it loops until one of the two conditions is met - either the user toggles the FFF0 switch or the user changes the value in memory location 1000h. Toggling th FFF0 switch terminates the program. Changing the value in memory location 1000h transfers control to a section of the program that adds together n words, where n is the new value in memory location 1000h."

after it sums up these values, it prints their 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

anyway, the problem is when I put the value 12h on 1000h, the program outputs 2 values, the sum, and the number 1.

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

I could'nt figure the cause of this.

thanks for the help

A: 

What environment are you running this in? Is it something in that environment that writes the extra output? Test that by comment out the put, and then run the program.

Can we see the code for put?

Btw, since you are writing it in assembler:

Instead of

mov cx,0

use

xor cx,cx

IIRC it saves some clock cycles (it was a very long time since I wrote assembler)

some
it does indeed save clock cycles.. but unless you have all the truth tables memorized, it's not always obvious what it's doing :)
warren
I agree that it's not that obvious the first time you see it, but neither is assembler language !
some
If you are programming assembler you should immediately recognize what this idiom does.
Svante
Also, programming assembler is not about writing clear code and relying on the compiler to optimize.
Svante
While you're at it replace sub cx,1 with dec cx. I believe that saves a byte. add bx,2 can be replaced with a pair of inc bx for the same bytes, but a performance increase on some antique machines, *if* the pipeline is filled...
Brian Knoblauch
...and neither the answer nor the comments are helpful in resolving the issue at hand
Nathan Fellman
@Nathan: ...and how is your comment helpful?
some
Mine is more of an observation :)
Nathan Fellman
...and it's not like I voted you down...
Nathan Fellman
@Nathan: Sorry, forgot a smiley after the last comment. You made me realize that I should have put my questions in the comments to the original question instead of creating an answer.
some
@some no worries. I thought that was implied.
Nathan Fellman