x86 assembly print register ascii -


i realize there lot of questions/answers how output integer in decimal, ascii form. i've taken code , modified own needs, instead of printing number, continues printing gibberish characters, , windows tells me program stopped working. think problem keeps popping values of stack though should break out of loop. here full 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 base dd 10 ans dd ?  .code start: mov ecx,3    ;i'm writing compiler using push ecx     ;jack crenshaw's "let's build compiler!" mov ecx,9    ;this sample output put in add ecx,[esp];the answer prints out should 42 push ecx mov ecx,2 xor edx,edx pop eax idiv ecx mov ecx,eax push ecx mov ecx,7 imul ecx,[esp]  mov eax,ecx xor ecx,ecx separatedigit:  xor edx,edx idiv base push edx inc ecx cmp eax,0 jne separatedigit  printdigit:  mov ans,0 pop ans dec ecx add ans,'0' invoke stdout,addr ans cmp ecx,0 jne printdigit  invoke exitprocess, 0 end start 

can hasn't been staring @ hours chime in , tell me i'm doing wrong?

the invoke call not "register safe" need preserve ecx value:

printdigit:      mov ans,0     pop ans     dec ecx     add ans,'0'     push ecx     ; save ecx     invoke stdout,addr ans     pop ecx      ; restore ecx     cmp ecx,0     jne printdigit 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -