我将非常感谢以下代码提供的一些帮助。该代码应该为表格,对象的数量和对有多少负值和奇数值的数量进行引用。 然后它应该将负值的数量发送到存储器中的一个表,并将奇数值的数量发送到存储器中的另一个表。 我尝试比较为零,我尝试找到最后一位,我尝试了注册表的xor,没有任何作用。重要的是所有值都是很长的类型。
.type max_1_ind, @function
.global max_1_ind
max_1_ind:
movq %rdi, %r8 # object table address from rdi to r8
xor %r11, %r11
xor %r12, %r12
movq %rcx, %r14 # rcx is needed for the loop
movq %rsi, %rcx # number of objects for the loop counter
xor %r15, %r15
loop:
movq (%r8), %r15
and $1, %r15
jc increase
continue:
movq (%r8), %r15
test %r15, %r15
jne increase2
continue2:
inc %r8
dec %rcx
cmpq $0, %rcx
jne loop
movq %r11, (%rdx)
movq %r14, %rcx
movq %r12, (%rcx)
finish:
xor %rax, %rax
ret
increase:
inc %r11
jmp continue
increase2:
inc %r12
jmp continue2
" c"调用此函数的程序
#include <stdio.h>
#include <stdlib.h>
extern long long max_1_ind(long long *tab, long long n, long long *even_cnt, long long *neg_cnt);
int main(void) {
long long tab[4] = {6, 2, -20, 3};
long long count = 4;
long long even_cnt[1] = {0};
long long neg_cnt[1] = {0};
max_1_ind(tab, count, even_cnt, neg_cnt);
printf("Result: %lld, %lld\n", *even_cnt, *neg_cnt);
return 0;
}
答案 0 :(得分:0)
感谢您的深刻回答。是的,我犯了inc。现在代码正常工作。
#include <stdio.h>
#include <stdlib.h>
extern long long max_1_ind(long long *tab, long long n, long long *even_cnt, long long *neg_cnt);
int m(long long * tab, long long n, long long *even_cnt, long long *neg_cnt){
int counter = n;
while(counter>-1){
//printf("%lld \n",tab[counter]);
if(tab[counter]<0){
neg_cnt[0]++;
}
if ((tab[counter] & 1) ==0){
even_cnt[0]++;
}
counter--;
}
return 0;
}
int main(void) {
long long tab[4] = {6, 40, -20, -3};
long long count = 4;
long long even_cnt[1] = {0};
long long neg_cnt[1] = {0};
max_1_ind(tab, count, even_cnt, neg_cnt);
printf("Wynik Asembler: %lld, %lld\n", *even_cnt, *neg_cnt);
return 0;
}
汇编代码:
.type max_1_ind, @function
#long long max_1_ind(long long *tab, long long n, long long *even_cnt, long long *neg_cnt);
.global max_1_ind
# %rdi, %rsi, %rdx, %rcx, %r8 and %r9
max_1_ind:
movq %rdi, %r8
xor %r11, %r11
xor %r12, %r12
movq %rsi, %rcx
xor %r15, %r15
loop:
movq (%r8), %r15
andq $1, %r15
jz increase
continue:
movq (%r8), %r15
testq %r15, %r15
js increase2
continue2:
addq $8, %r8
dec %rcx
cmpq $0, %rcx
jne loop
movq %r11, (%rdx)
movq %r14, %rcx
movq %r12, (%rcx)
finish:
xor %rax, %rax
ret
increase:
inc %r11
jmp continue
increase2:
inc %r12
jmp continue2