0
\$\begingroup\$

I was trying to solve a problem and I read that this code converts a number from hex to decimal I mean why . My idea is that div is just for division can somebody answer my questions that I left as comments

MOV A,#0FFH ;why do we need to do this MOV P1,A ;P1 as an INPUT MOV A,P1 ;Get data from P1 MOV B,#10 ;to convert from hex to decimal DIV AB ;that the question why does div used to do this conversion MOV R7,B ;does this put 10 in the register r7 MOV B,#10 DIV AB MOV R6,B ;it does also puts 10 in the register r6? MOV R5,A ;if A <10 (why does this do )

\$\endgroup\$
6
  • \$\begingroup\$ Could you please revert that edit? I had the formatting fixed. A picture is really not a good way to show your code. \$\endgroup\$
    – JRE
    Commented Apr 27, 2017 at 18:01
  • \$\begingroup\$ It'd help to state which assy lang. this is, but DIV AB appears to be integer dividing A by B, then putting the remainder in B. Thus, for FF you'd have R7=5, R6=5, and R5=2. \$\endgroup\$ Commented Apr 27, 2017 at 18:01
  • \$\begingroup\$ @JRE check edit log...y'all were having an edit war. \$\endgroup\$ Commented Apr 27, 2017 at 18:02
  • \$\begingroup\$ @RobhercKV5ROB: yeah, it was frustrating to fix the coding only for it to be mangled again. \$\endgroup\$
    – JRE
    Commented Apr 27, 2017 at 18:04
  • \$\begingroup\$ Tried to but it gets worse - I had it formatted nicely like MCS-51 assembly an hour or so ago. \$\endgroup\$
    – TonyM
    Commented Apr 27, 2017 at 19:38

1 Answer 1

5
\$\begingroup\$

Simple enough

A starts out being a number between zero and 255.

Lets say its FF i.e. 255..

Dividing by 10 gets you 25 in A and 5, the remainder, in B. That's the units value, so we save B to R7

Next we setup B with 10 again and divide the 25 by ten, and again store the remainder, this time in R6, as the tens digit.

What is left in A is the hundreds count, and we store that in R5.

So.. R5,6, and 7 now contain 2,5,5, the decimal equivalent of FF.

Here is a register trace if it helps..

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Now I can agree with it & +1 ;) \$\endgroup\$ Commented Apr 27, 2017 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.