简体   繁体   English

8051 汇编程序将值从一个范围复制到另一个范围,保持奇数值并将偶数值转换为 BCD

[英]8051 assembler program to copy values from a range to another maintaining odd values and transforming even values to BCD

I need to do this task:我需要做这个任务:

Write the program in 8051 assembler which copies the memory range 30H – 3FH to the memory range 40H – 4FH, copying the odd values without a change and the even values converted to the BCD format.在 8051 汇编程序中编写程序,该程序将 memory 范围 30H – 3FH 复制到 memory 范围 40H – 4FH,复制奇数而不更改,偶数转换为 BCD 格式。 Do not use any subroutines.不要使用任何子程序。

Right now I have a program that saves the odd values but skips the even values:现在我有一个程序可以保存奇数值但跳过偶数值:

MOV  R0,#30H
MOV  R1,#40H
LOOP:
MOV    A,@R0
ANL  A,#00000001B
JZ NOT_INTERESTED 
MOV  A,@R0
MOV  @R1,A
INC  R1
NOT_INTERESTED:
INC  R0
CJNE R0,#40H,LOOP

and I have a program that converts values to BCD:我有一个将值转换为 BCD 的程序:

MOV    30H,#63
MOV    A,30H
CALL HEX_2_BCD
MOV  31H,A
HEX_2_BCD:
MOV  B,#10
DIV  AB
SWAP A
ADD  A,B
RET

Do you know how I can combine them to get the needed result?你知道我如何将它们结合起来以获得所需的结果吗?

 MOV 30H,#63 MOV A,30H CALL HEX_2_BCD MOV 31H,A HEX_2_BCD: MOV B,#10 DIV AB SWAP A ADD A,B RET

The code that converts to BCD is wrong.转换为 BCD 的代码是错误的。 Upon returning from CALL HEX_2_BCD , the A register holds the BCD.CALL HEX_2_BCD返回后, A寄存器保存 BCD。 Fine, but then the code needlessly falls through in the conversion code.很好,但随后代码不必要地落在了转换代码中。 This is not very exemplary.这不是很典型。 Nonetheless, you can extract from it the essential instructions that perform the BCD conversion:尽管如此,您可以从中提取执行 BCD 转换的基本指令:

  MOV  B, #10
  DIV  AB
  SWAP A
  ADD  A, B

Because in the new program every byte from the source range will have to be written to the destination range, it will be easiest if we conditionally jump for the bytes that have an odd values and that can be written unmodified.因为在新程序中,源范围中的每个字节都必须写入目标范围,如果我们有条件地跳转到具有奇数值并且可以不加修改地写入的字节,那将是最简单的。
Furthermore, if we modify the test for even/odd a bit, we wouldn't have to reload the A register.此外,如果我们稍微修改一下偶数/奇数的测试,我们就不必重新加载A寄存器。 Remember the A register is a Special Function Register whose bits are bit addressable using bit addresses 0E0h to 0E7h.请记住, A寄存器是一个特殊的 Function 寄存器,其位可使用位地址 0E0h 到 0E7h 进行位寻址

  MOV  R0, #30h
  MOV  R1, #40h
Loop:
  MOV  A, @R0         ; Read from source
  JB   0E0h, IsOdd    ; Go write unmodified
IsEven:
 
  ...

IsOdd:
  MOV  @R1, A         ; Write to destination
  INC  R1
  INC  R0
  CJNE R0, #40h, Loop

It's probably a good idea to verify if the number that you want to convert to BCD is less than 100, else the converted result will be kinda worthless!验证要转换为 BCD 的数字是否小于 100 可能是个好主意,否则转换后的结果将毫无价值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM