[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
x86ASMFAQ_Problems
How do I print a number?

The best answer to this question is probably to use C's printf function. See other languages for an example of how to use functions from other languages.

However, here is a simple snippet to convert the value in eax to a decimal string (the printing of the string is left to you, as it is operating system specific). edi is assumed to be pointing at a buffer to hold the string.
    push edi                ;save edi
    mov ebx, 10             ;we're dividing by 10
    xor ecx, ecx            ;ecx=0
while_eax_is_not_0:
    inc ecx                 ;count how many characters
    xor edx, edx            ;edx=0
    div ebx                 ;divide edx:eax by ebx
    add dl, '0'             ;edx contains eax mod ebx and we convert to ASCII
    push edx                ;save it on the stack
    cmp eax, 0              ;is eax zero?
    jne while_eax_is_not_0  ;if not repeat
for_each_character:         ;now we pop off the characters to reverse them
    pop eax                 ;get last character first
    stosb                   ;store it into the string
    dec ecx                 ;decrement ecx
    jnz for_each_character  ;is ecx zero? if not repeat
    mov al, 0               ;clear al for NUL terminating byte
    stosb                   ;save it
    pop edi                 ;restore edi (which now points at the string) 


How do I use functions from other languages?

Most assemblers have directives to declare certain symbols as external. Once declared external you'd use the function as you normally would using the calling conventions of the language (C's will be used as an example and as it is the most common). Some assemblers (e.g. TASM) also have directives to automatically handle this (which WILL NOT be used here). It is quite likely that you'll need to mangle the name of the symbol for it to work correctly, once again those are specific to the language you are trying to link in. Once you've assembled the file, simply link the object file containing the external function with the object file containing your assembly code.

TASM:
;declare an external procedure printf and mangle it with an underscore (_)
EXTRN _printf: PROC 
                                ;C calling convention
    push 10                     ;push variables from right 
    push OFFSET formatstring    ;to left
    call _printf                ;so this would be printf(formatstring,10);
    add esp, 8                  ;caller cleans up the stack


Why do I get a divide overflow?

Divide overflows happen when you try to divide a big number by a small number and the result doesn't fit in the destination operand. For example, trying to divide 2^56 by 1 will give you a result that needs 56 bits, but the destination operand is only 32. However, this question is targetted at a more common problem, you are only trying to divide 50 by 3! Why is it overflowing? The most likely culprit is that you didn't clear (e)dx, before the division. When you divide you divide the value (e)dx:(e)ax. If you leave something in (e)dx then it could result in overflows and it almost surely will result in wrong answers. Therefore, before any division you should clear (e)dx unless you know for certain it is clear, or it's part of the number.

What does this do?

Do you see one or two line assembly snippets everywhere that everyone seems to use and you have no clue what it does? Then this is the section for you. This section contains the mini-idioms that are prevalent in assembly.

''Conventions:''
Unless otherwise specified where eax is used, any other register will suffice (e.g. edx, cl, or bx).
N is some arbitrary immediate (within reason), e.g. mov eax, N could be mov eax, 5.

  • xor eax, eax: clears eax
  • sub eax, eax: clears eax
  • shl eax, N: multiply eax by 2N
  • shr eax, N: unsigned integer divide by 2N
  • sar eax, N: signed integer divide by 2N
  • and eax, 2N-1: mod by 2N (e.g. and eax, 255)
  • test eax, 1: test if eax is odd
  • test eax, eax: test if eax is 0


last edited (January 4, 2003) by Darius, Number of views: 3157, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.