Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MOD in assembly 1

Status
Not open for further replies.

paulbradley

Programmer
Oct 9, 2002
158
0
0
GB
How do you program to find mod 10 of a number in assembly? In total i need to output a number held in a registered to the screen so need to mod 10, add 30 and put on a stack to be popped off in reverse.
 
Try AAM:

The aam (ASCII Adjust after Multiplication) instruction, like aaa and aas, lets you adjust an unpacked decimal value after multiplication. This instruction operates directly on the ax register. It assumes that you've multiplied two eight bit values in the range 0..9 together and the result is sitting in ax (actually, the result will be sitting in al since 9*9 is 81, the largest possible value; ah must contain zero). This instruction divides ax by 10 and leaves the quotient in ah and the remainder in al:

ah := ax div 10 al := ax mod 10
Unlike the other decimal/ASCII adjust instructions, assembly language programs regularly use aam since conversion between number bases uses this algorithm.

Note: the aam instruction consists of a two byte opcode, the second byte of which is the immediate constant 10. Assembly language programmers have discovered that if you substitute another immediate value for this constant, you can change the divisor in the above algorithm. This, however, is an undocumented feature. It works in all varieties of the processor Intel has produced to date, but there is no guarantee that Intel will support this in future processors. Of course, the 80286 and later processors let you multiply by a constant, so this trick is hardly necessary on modern systems.
Art of Assembly Language Programming book.
 
When you use the div and idiv instructions, the remainder is left in dx.
For instance, if you start with ax=123 and divide by ten, you end up with ax=12, dx=3. dx is therefore the mod part, and ax the div part.

To be honest, I'm quite a fan of div and idiv. They're well-supported instructions that have been getting faster and faster with each successive processor, and they're reasonably portable in that their function is not obscure, and likely to be implemented in many families of processor in the future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top