正,负和零(组装)

编程入门 行业动态 更新时间:2024-10-28 02:32:56
本文介绍了正,负和零(组装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编程一个旧的MCU(68hc11),我正在尝试使用68hc11指令从C语言迁移到汇编代码.

I'm programming an old MCU (68hc11), I'm trying to migrate from C language to assembly code using the 68hc11 instructions.

我想编写一个汇编程序,该程序对给定数组中存在的POSITIVE,NEGATIVE和ZERO值进行计数.请注意,ARRAY中的所有值都可以是全正或全负或全零,您明白了吗?因此,我应该定义将正确存储数量的变量的大小.

I want to write a program in assembly that counts the number of POSITIVE, NEGATIVE, and ZERO values that exist inside a given array. DO NOTE that all values inside ARRAY could be all positive or all negative or all zeros,do you get me? So I should define the size of the variables that will store the quantity properly.

注意:数组的结尾是:ARRAY + QUANTITY-1

NOTE: The end of the array is: ARRAY+QUANTITY-1

数组:包含一些随机值

Array: contains some random values

QUANTITY:代表阵列最多可以包含的元素数量 按住

QUANTITY: represent the highest number of elements that the ARRAY can hold

我用C语言编写了这个程序:

I wrote this program in C:

int A[15], pos, neg, nul, i; [...] pos = 0; neg = 0; nul = 0; for (i = 0; i < 15; i++) { if (A[i] > 0) { pos++; } if (A[i] < 0) { neg++; } if (A[i] == 0) { nul++; } }

现在,我想翻译它,但是在汇编中(我在这里卡住了,我没有得到想要的东西)

Now, I want to translate that but in assembly (I GOT STUCK IN HERE, i'm not getting what i want)

RWM EQU $0 ROM EQU $C000 VRESET EQU $FFFE QUANTITY EQU 800 ;MEANS THE MAXIMUM AMOUNT OF VALUES THAT THE ARRAY WILL CONTAIN ORG RWM POSITIVE RMB 2 NEGATIVE RMB 2 ZEROS RMB 2 ORG ROM START: CLRA CLRB CLR POSITIVE CLR ZEROS CLR NEGATIVE LDY #ARRAY LOOP CPY #(ARRAY+QUANTITY-1) BHI END LDD 0,Y INY BLT NEGATIVE_NUMBER BEQ ZERO_NUMBER BGE POSITIVE_NUMBER NEGATIVE_NUMBER INC NEGATIVE BRA LOOP POSITIVE_NUMBER INC POSITIVE BRA LOOP ZERO_NUMBER INC ZEROS BRA LOOP END BRA END ARRAY DW 78,554,-44,-4,2547,0,-3,0,1,7,8, ORG VRESET DW START

我的代码怎么了?

RWM EQU $0 ROM EQU $C000 VRESET EQU $FFFE QUANTITY EQU 800 ;MEANS THE MAXIMUM AMOUNT OF VALUES THAT THE ARRAY WILL CONTAIN ORG RWM POSITIVE RMB 2 NEGATIVE RMB 2 ZEROS RMB 2 ORG ROM START: CLRA CLRB CLR POSITIVE CLR ZEROS CLR NEGATIVE LDY #(ARRAY-2) LOOP INY INY CPY #(ARRAY+2*QUANTITY-1) BHI END LDD 0,Y BLT NEGATIVE_NUMBER BEQ ZERO_NUMBER BGE POSITIVE_NUMBER NEGATIVE_NUMBER INC NEGATIVE BRA LOOP POSITIVE_NUMBER INC POSITIVE BRA LOOP ZERO_NUMBER INC ZEROS BRA LOOP END BRA END ARRAY DW 78,554,-44,-4,2547,0,-3,0,1,7,8, ORG VRESET DW START

我得到了这个输出(不确定)

I got this output (not sure about it)

推荐答案

我的代码怎么了?

What's wrong with my code?

我只是快速浏览了您的程序,所以我的答案可能是错误的:

I only had a quick look at your program, so my answer may be wrong:

LDD 0,Y

很明显,您使用的是16位数字.否则,您将使用LDA或LDB代替LDD.

Obviously you are operating with 16 bit numbers. Otherwise you would use LDA or LDB instead of LDD.

CPY #(ARRAY+QUANTITY-1)

如果QUANTITY是数组大小(以字节为单位)(例如,对于int A[15],则为30),这将是正确的.

This would be correct if QUANTITY is the size of the array in bytes (e.g. 30 in the case of int A[15]).

如果QUANTITY是元素数并且您使用16位数字进行操作,则必须乘以2:

If QUANTITY is the number of elements and you are operating with 16-bit numbers, you have to multiply with two:

CPY #(ARRAY+2*QUANTITY-1)

INY

再次适用于8位元素.对于16位元素,您必须在每个元素上添加两个或将Y递增两次:

Once again this would work for 8-bit elements. For 16-bit elements you have to add two to each element or to increment Y twice:

INY INY

INY BLT NEGATIVE_NUMBER

INY指令将修改零标志,因此会影响BLT,BGE和BEQ指令.您不能在LDD和分支指令之间使用INY.

The INY instruction will modify the zero flag and therefore influence the BLT, BGE and BEQ instructions. You cannot use INY between LDD and the branch instructions.

BLT ... BEQ ... BGE ...

并不是真正的错误,但是BGE指令将始终分支.您可以在BEQ指令之后放置POSITIVE_NUMBER数字代码,然后删除BGE指令.

Not really an error, but the BGE instruction will always branch. You may place the POSITIVE_NUMBER number code after the BEQ instruction and remove the BGE instruction.

编辑

我对这部分不了解:INY后跟Bxx

我正在考虑三种不同的方式来做到这一点.最简单的一个似乎是:

I was thinking about three different ways to do this. The simplest one seems to be:

LDY #(ARRAY-2) ; Because we'll do INY twice right now LOOP INY INY CPY #(ARRAY+2*QUANTITY-1) BHI END LDD 0,Y BLT NEGATIVE_NUMBER ...

更多推荐

正,负和零(组装)

本文发布于:2023-11-22 09:07:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!