Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

assembly - 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. 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:

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?

question from:https://stackoverflow.com/questions/65919121/8051-assembler-program-to-copy-values-from-a-range-to-another-maintaining-odd-va

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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. Upon returning from CALL HEX_2_BCD, the A register holds the 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:

  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. Remember the A register is a Special Function Register whose bits are bit addressable using bit addresses 0E0h to 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...