1

I'm studying assembly language from the book "art of x86 assembly" and I have a question I couldn't 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 memory. then you will switch to the memory screen and modify values in memory (that is, you will directly access memory while the program continues to run).

the program begins by setting memory 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

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).

Can someone please explain this behavior?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 4
    how is `put` defined? I don't recognize that as an assembly instruction. – Nathan Fellman Nov 20 '08 at 09:53
  • 1
    This question appears to be off-topic because it is too localized. No answers in 5 years. – some Jan 14 '14 at 02:45
  • Unanswerable without knowing how you single-step through it. Unless your debugger breaks the program, you should get the same result from running as from single-stepping. Also, this program is super-bloated and with weird branching. If you only want to print once at the end, why is there a `jmp` back to the top after the `put`? – Peter Cordes Mar 22 '18 at 04:09
  • I think this code is for `debug.exe`. It treats all constants as hex, which would explain using `[1000]` and then talking about the memory location `1000h`. Other assemblers would treat `1000` as 1000 decimal. – Peter Cordes Mar 22 '18 at 04:12
  • debug.exe doesn't support labels though – Michael Petch Mar 22 '18 at 05:50
  • 1
    @PeterCordes debug.exe doesn't support labels though. This was a [simx86](http://nicolascormier.com/documentation/sys-programming/binary_formats/elf/extending_sim286_to_the_intel386_architecture_with_32-bit_processing_and_elf_binary_input/node8.html) related question. Appears the new answer is quite the necromancy feat lol – Michael Petch Mar 22 '18 at 06:04
  • @MichaelPetch: yeah, seriously. I voted to close it as lacking a [mcve], because the OP doesn't say what they did differently as far as setting values at `[1000h]` and `[0fff0h]` while it was running. (Although obviously it was different, and the program is designed to respond to external setting of memory locations by an interrupt handler or debugger.) Mostly it's just not a very useful question and should probably go away, IMO. – Peter Cordes Mar 22 '18 at 06:28

1 Answers1

0

I interpretted the code, hope it helps someone:

Note: This code does not consider multi-threading or interrupts changing the registers. It is a straight-forward top-down analysis (to help a beginner, not a 'pundit').

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)
Mr. de Silva
  • 382
  • 2
  • 11
  • typo in `sub cx,1 ; Dedict 1 from bx`: wrong register. This barely a useful answer, it doesn't trace the execution at all. Also, the cx=0 comment on the load at label `a:` is wrong: it's a volatile MMIO register or shared variable that doesn't necessarily read back what you stored; the program waits for it to be modified (by another thread or interrupt handler) – Peter Cordes Mar 22 '18 at 06:23
  • You are overthinking it, this was not a post to get a job for myself (which seem like what most people here are looking for - career advancement..?) This does not assume multi-threading, etc, it is a single thread execution for someone to figure it out. FYI: I have designed microprocessors in my time :) – Mr. de Silva Mar 22 '18 at 11:32
  • Read the question again. This is an infinite loop until `[1000]` is asynchronously modified, and the question specifically describes that being done. (Or it exits right away if `[fff0]` is 0.) (And BTW, the person who posted it nearly a decade ago has since deleted their account. It is a useful partial answer for beginners that don't know how to read individual x86 asm instructions, but I suspect not many of them will find this question.) – Peter Cordes Mar 22 '18 at 11:51
  • Yes, I noticed that the question was old way after I answered it, I have not coded hard-core assm since 1991.... I will look at the question later and edit the answer or delete it. Also, I only analyzed each line - not the solution - that was not my intention. – Mr. de Silva Mar 22 '18 at 11:58
  • Welcome to Stack Overflow :) I usually look for questions to answer by searching on a bunch of tags at once, so I see recently-active questions in those tags all on one page. Whether that's newly asked, or edited or answered. My current search bookmark is https://stackoverflow.com/questions/tagged/x86+or+x86-64+or+assembly+or+sse+or+sse2+or+avx+or+avx2+avx512+or+intel+or+simd+or+cpu-architecture+or+micro-optimization+or+intrinsics+or+inline-assembly+or+low-level+or+perf+or+benchmarking+or+lock-free+or+stdatomic?sort=active&pageSize=30. – Peter Cordes Mar 22 '18 at 12:21