复制教材x86汇编数组示例遇A2008语法错误(cqd、indiv指令)
cqd and indiv Hey there! Let's sort out those syntax errors you're hitting with your array program. Both issues come down to using invalid x86 instruction names—here's what's wrong and how to fix it:
1. Error: syntax error : cqd
The instruction you're trying to use (cqd) doesn't exist in standard x86 assembly. Your comment says you want to "extend sum to quadword"—so you need to pick the right instruction based on your target bit width:
- For 16-bit code: Use
cwdto extend the 16-bit sum inAXto the 32-bit pairDX:AX. - For 32-bit code: Use
cdqto extend the 32-bit sum inEAXto the 64-bit pairEDX:EAX. - For 64-bit code: Use
cqoto extend the 64-bit sum inRAXto the 128-bit pairRDX:RAX.
Swap out cqd for the correct instruction matching your assembly environment, and this error will vanish.
2. Error: syntax error : indiv
There's no such thing as an indiv instruction in x86! Your comment mentions calculating the average, so you need either:
div: For unsigned integer division. Remember, for 32-bit code, it usesEDX:EAXas the dividend, so you'll need to zero outEDXfirst if you're doing unsigned division (usexor edx, edx).idiv: For signed integer division. This uses the extended dividend fromcdq/cwd/cqo, so make sure you run the extension instruction right beforeidiv.
Example Fixed Code Snippet
Here's how your code might look if you're working in 32-bit signed mode:
; ... your array summation loop here ... cdq ; Extend sum in EAX to EDX:EAX (correct replacement for cqd) idiv ecx ; Calculate average: EDX:EAX divided by ECX (quotient in EAX)
For unsigned 32-bit mode, it would be:
; ... your array summation loop here ... xor edx, edx ; Zero out EDX for unsigned division div ecx ; Calculate unsigned average
Just double-check that your instruction matches your data sizes and signed/unsigned needs, and you'll be good to go.
内容的提问来源于stack exchange,提问作者Some_Dude

