盘点编号(HC11) [英] Counting number (HC11)

查看:140
本文介绍了盘点编号(HC11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在使用此MC

现在,我要在给定数组中对正/负数和0进行计数.在c语言中,我做了这样的事情,并且效果很好:

Now i want to count positive/negative numbers and 0's in a given array. In c, i did something like this and it worked perfectly:

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++;
    }
}

因此,下一步是进行类似的操作,但是在汇编代码中,我正在考虑以下问题:

So, the next step is to make something similar but in assembly code, i was thinking about this:

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

我有点困惑,因为我需要考虑 最坏的情况,我的意思是:全部为正,或全部为负,或全部为零. 所以,我应该根据要定义的信息来定义变量大小 已保存. 我认为数组的末尾应为ARRAY + QUANTITY-1.

i'm a little confused right here because i would need to consider the worst cases, i mean: all are positive, or all negative or all zero. So, i should define variable sizes according to the information to be saved. I think the end of the array should be ARRAY + QUANTITY-1.

EDIT#1:

在这种情况下,我想获得此输出:

For this case i would like to obtain this output:

由于数组包含以下元素:

Since th ARRAY contains these elements:

1,4,8,-87,0,0,1,4,5,8,7,4,4,1,-9

我应该得到以下输出:

POSITIVES       11  
NEGATIVES       2   
ZEROS           2

但是请记住:

我必须考虑最坏的情况,即:所有人都是积极的,或者都是 负数或全零

i must consider the worst cases, i.e: all are positive, or all negative or all zero


另一种情况:

假设我要获取存储在特定数组中的所有元素的绝对值.

Suppose that i want to obtain the absolute values of all the elements that are stored in a specific array.

我可以使用'C'来实现,我的意思是,我可以执行以下操作:

I can achieve that using 'C',i mean, i could perform something like this:

#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);

}

我试图在汇编中做到这一点(使用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

我想将 ARRAY

I want to store inside ABSOLUTE all the absolute elements from ARRAY

PS:我尚未定义绝对尺寸

PS: i have not defined the size for ABSOLUTE

我想在ABSOLUTE中看到这些值:

I would like to see these values inside ABSOLUTE:

4,144,447,14,555,1147(无符号数字)

4,144,447,14,555,1147 (UNSIGNED NUMBERS)

推荐答案

在您的示例中,将QUANTITY的定义定义为200似乎毫无意义,因为您对数组进行了硬编码,因此无论QUANTITY是什么,它都具有已知数量的元素.说.最好让汇编程序将QUANTITY定义为实际的元素数量,如下所示(但在我的基于ASM11的示例中未使用).

The definition of QUANTITY as 200 seems pointless in your example because you hard code your array so it has a known number of elements regardless of what QUANTITY says. It would be better to have the assembler define QUANTITY to the actual number of elements like shown below (but not used in my ASM11 based example).

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代码不正确.我想你打算写这样的东西:

BTW, your C code is incorrect. I think you meant to write something like this:

#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;
}

这篇关于盘点编号(HC11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆