Search code examples
assemblyx86masm32

x86 assembly (masm32) output multiplied number produces junk characters


I'm coming back to assembly for the sake of it after a few months and I'm having trouble getting two numbers to multiply and output the result. Here's my code:

.386
.model flat, stdcall 
option casemap :none 

include \masm32\include\windows.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\masm32.inc 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\masm32.lib

.data 
     sum sdword 0
.code 
start:
mov ecx, 6        
xor eax, eax                  
mov edx, 7               
mul edx                  
push eax
pop sum
lea eax, sum
call StdOut
push 0 
call ExitProcess
end start 

It outputs something like P &aeffiini,.

QUESTION: Why does it output that random character string, and how can I fix it?

Thanks in advance.


Solution

  • Because StdOut prints NULL terminated strings NOT numbers. You need to convert the number to a string first. MASM32 has dwtoa. Also, your multiplying wrong. you multiply with eax

    include masm32rt.inc
    
    .data?
    lpBuffer    db 12 dup (?)
    
    .code 
    start:
        mov     ecx, 6        
        mov     eax, 7               
        mul     eax    
    
        push    offset lpBuffer
        push    eax
        call    dwtoa 
    
        push    offset lpBuffer             
        call    StdOut
    
        inkey
        push    0 
        call    ExitProcess
    end start