我还在和这个主持人玩
现在我要计数给定数组中的正负数和0。在c中,我做了这样的事情,它非常有效:
int A[15], pos, neg, nul, i;
[...]
pos = 0;
neg = 0;
nul = 0;
for for (i = 0; i < 15; i++) {
if (A[i] > 0) {
pos++;
}
if (A[i] < 0) {
neg++;
}
if (A[i] == 0) {
nul++;
}
}所以,下一步是做一些类似的事情,但在汇编代码中,我在想:
RWM EQU $0
ROM EQU $C000
RESET EQU $FFFE
QUANTITY EQU 200
ORG RWM
POSITIVES RMB
NEGATIVES RMB
ZEROS RMB
ORG ROM
Main:
END BRA END
ARRAY DW 1,4,8,-87,0,0,1,4,5,8,7,4,4,1,-9
ORG RESET
DW Main,我在这里有点困惑,因为我需要考虑最坏的情况,我的意思是:所有的都是正的,所有的都是负的或全部为零。因此,我应该根据要保存的信息定义可变大小。我认为数组的结尾应该是数组+数量-1。
EDIT#1:
在这种情况下,我想获得以下输出:
由于th数组包含以下元素:
1,4,8,-87,0,0,1,4,5,8,7,4,4,1,-9我应该得到这个输出:
POSITIVES 11
NEGATIVES 2
ZEROS 2但请记住:
I必须考虑最坏的情况,即:所有的情况都是正的,所有的都是负的,或者都是零的。
另一个不同的案例:
假设我希望获得存储在特定数组中的所有元素的绝对值。
我可以用“C”来达到这个目的,我的意思是,我可以这样做:
#include <stdio.h>
#include <math.h>
int absolute(int *array, int N);
int main()
{
int array[16] = {0,1,2,3,-4,5,6,7,-8,9,-10,11,12,13,14,20};
int ray[16];
int i;
for ( i = 0; i < 16; i++ )
ray[i]=absolute(array,16);
printf("the absolute value is %d\n", ray[i]);
return 0;
}
int absolute(int *array, int N)
{
int i;
for(i=0; i<N; i++)
if (array[i]<0)
array[i] = array[i] * (-1);
}I试图在程序集(使用68hc11指令)中实现这一点。
RWM EQU $0
ROM EQU $C000
RESET EQU $FFFE
ORG RWM
ABSOLUTE RMB
ORG ROM
Start:
END BRA END
ARRAY DW 4,144,447,-14,-555,-1147
ORG RESET
DW Start我想在绝对中存储数组中的所有绝对元素
PS:我还没有定义绝对的大小
我希望看到这些值是绝对的:
4,144,447,14,555,1147 (无符号号码)
发布于 2020-06-11 22:52:01
在您的示例中,将QUANTITY定义为200似乎毫无意义,因为您对数组进行了硬编码,因此不管QUANTITY说什么,它都有已知数量的元素。最好让汇编程序将QUANTITY定义为如下所示的实际元素数(但不在基于ASM11的示例中使用)。
RAM equ $0
ROM equ $C000
Vreset equ $FFFE
;*******************************************************************************
#ROM
;*******************************************************************************
org ROM
ARRAY dw 4,144,447,-14,-555,-1147
;QUANTITY equ *-ARRAY/2
;*******************************************************************************
#RAM
;*******************************************************************************
org RAM
absolute rmb ::ARRAY
zeros rmb 1
positives rmb 1
negatives rmb 1
;*******************************************************************************
#ROM
;*******************************************************************************
Start ldx #ARRAY ;X -> source
ldy #absolute ;Y -> destination
;-------------------------------------- ;initialize all counters to zero
clr zeros
clr positives
clr negatives
;--------------------------------------
Loop ldd ,x ;D = word to test
beq CountZero ;go count zero
bpl CountPositive ;go count positive number
;--------------------------------------
inc negatives ;count negative number
; negd ;make it positive (abs)
coma
comb
addd #1
bra Cont
;--------------------------------------
CountZero inc zeros
bra Cont
;--------------------------------------
CountPositive inc positives
; bra Cont
;--------------------------------------
Cont std ,y ;save absolute value
ldab #2 ;B = word size
abx ;X -> next word
aby ;Y -> next word
cpx #ARRAY+::ARRAY ;check for end of array
blo Loop ;repeat for all elements
bra *
org Vreset
dw Start顺便说一句,你的C代码不正确。我想你是想写这样的东西:
#include <stdio.h>
#define SIZE 16
int absolute(int array[], int ray[], int N)
{
for (int i=0; i<N; i++)
ray[i] = array[i] * (array[i]<0?-1:1);
}
int main()
{
int array[SIZE] = {0,1,2,3,-4,5,6,7,-8,9,-10,11,12,13,14,20};
int ray[SIZE];
absolute(array,ray,SIZE);
for (int i = 0; i < SIZE; i++ )
printf("The absolute value of %3i is %3i\n", array[i],ray[i]);
return 0;
}https://stackoverflow.com/questions/62313417
复制相似问题