8085汇编代码修改求助:从给定数字列表中分离奇数并存储至新列表
Let's break down why your current code captures even numbers instead of odd ones, then fix it step by step.
What's Wrong with the Original Code?
Your jump condition is reversed, which flips the logic:
ANI 01Hchecks the least significant bit (LSB) of the number. If the number is odd, the LSB is 1, soANI 01Hresults in01Hand the Zero Flag (Z) is cleared.JNZ SKIPmeans "Jump to SKIP if the Zero Flag is NOT set" — so when you have an odd number, you jump over the storage code and ignore it.- When the number is even, the LSB is 0,
ANI 01Hgives00H, the Zero Flag is set,JNZdoesn't trigger, so you store the even number instead.
Modified Code to Capture Odd Numbers
We just need to flip the jump condition to skip storage only when the number is even:
LXI H, 2200H ; Load source list starting address into HL pair LXI D, 2300H ; Load destination list starting address into DE pair MVI C, 32H ; Set counter to 50 (32H in hex) — adjust if your list size differs BACK: MOV A, M ; Move current number from source to accumulator A ANI 01H ; Check LSB: result is 1 if odd, 0 if even JZ SKIP ; Jump to SKIP if Zero Flag is set (i.e., number is even) MOV A, M ; Reload the original odd number into A (ANI modified the value) STAX D ; Store the odd number at the destination address INX D ; Increment destination pointer to next empty location SKIP: INX H ; Increment source pointer to the next number in the list DCR C ; Decrement the counter JNZ BACK ; Repeat until all numbers are processed HLT ; Halt the program
Key Change Explained
The only critical modification is replacing JNZ SKIP with JZ SKIP:
- Now, when the number is even (Zero Flag set), we jump to
SKIPand skip storing it. - When the number is odd (Zero Flag cleared), we proceed to store it in the destination list.
Quick Note on Reloading the Number
We have to reload the original number into A with MOV A, M after ANI 01H because the ANI instruction modifies the accumulator's value (turning it into 00H or 01H). Without this step, we'd store 01H instead of the actual odd number — so don't skip this line!
This modified code will correctly iterate through your source list at 2200H, pick out all odd numbers, and store them sequentially starting at 2300H.
内容的提问来源于stack exchange,提问作者YeoAar6789

