How does the Div10 algorithm Div10 in this program? The mathematical meaning of all these exchanges with xchg .

 ; DWToAscii преобразует 4-байтовое число в последовательность символов DWToAscii proc near call Div10 mov si, dx or si, ax jz done ; Записать первую цифру push bx call DWToAscii pop bx ; Достать очередную цифру done: add bl, '0' ; Перевести цифру в символ mov [di], bl ; Сохранить символ inc di ret DWToAscii endp Div10 proc near mov cx, 10 mov bx, ax xchg ax, dx xor dx, dx div cx xchg dx, bx ret Div10 endp 

We divide the four-byte number in dx:ax by 10. In the second line, save the low-order bits in bx , instead of the low-order bits, load the high-order bits, while at the same time saving them in dx (this is not clear: why save them if we have already saved them?). Reset the high bits. We divide by 10 the number 0:ax , where the ax now the high-order bits. Now in ax - the quotient, and in dx - the remainder, and we replace the remainder with the lower bits of the number. We somehow broke the number, but for some reason it did not affect the result. This is surprising, because the decomposition of a number by the base of a number system does not allow it to be used in any part of it.

Function call:

 start: mov dx, 12345 ; Старшие биты mov ax, 6789 ; Младшие биты mov cx, 10 mov di, offset digit call DWToAscii mov [di], byte ptr 0Dh inc di mov [di], byte ptr 0Ah inc di mov [di], byte ptr '$' mov dx, offset digit mov ah, 9h int 21h mov ah, 4ch int 21h 

enter image description here enter image description here enter image description here

  • No nonsense, it works. Here is the third form of the div instruction — it divides the double word written in dx: ax registers into a number in cx. So does the div. Added code to post. - typemoon
  • The dividend is transferred in two registers dx: ax. This is a task from the book - you need to implement on a 16-bit asme the output of a 4-byte number as a sequence of characters. To do this, use the stack and the auxiliary function of dividing by 10. - typemoon
  • Yes, this is a typo. - typemoon
  • 2
    @insolor But she can't work with a normal four-byte number only. she is prepared a number broken by the rules of the decimal number system. For 123456789 decimal in a normal four-byte format should look like hex 75BCD15 with two-byte parts in the decimal system 1883: 52501 - Mike
  • one
    @typemoon And what does the text in the book and your example have in common? they have two division operations, they transfer the remainder of the division of the higher part to the lower order for further work. And in your 16-bit variant, one single division occurs, the discharge is not transferred anywhere and pieces of the number from the highest part from the younger part are put on the stack, from where they are prepared for printing in the reverse order - Mike

0