News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Printing contents of a register

Started by llkooj, December 02, 2008, 12:13:58 AM

Previous topic - Next topic

llkooj

What is the syntax to print the contents of a register say ECX? I have tried many techniques using stdout without any success.

qWord

hi,

you need to convert the content of ecx to an ASCI-string be for showing it in console:

(use "Console Assemble & Link" in qeditor)


    include \masm32\include\masm32rt.inc
    .code
start:

    call main
    inkey
    exit

main proc

    cls
    print "ecx = "
    mov ecx,123456789
    print udword$(ecx),10,13 ; show ecx as unsigned dword (decimal)
    ret

main endp
end start


regards, qWord
FPU in a trice: SmplMath
It's that simple!

llkooj

Thanks for that solution. However, I never envisioned another problem. I am actually trying to print this register content several times in a loop. Each time the contents of the register changes. Only one value is printed and that was the 1st value.

llkooj

Note. I am using Visual Studio 2005. I debug the program and then I tried to use the command line to run the exe that was produced.

Mark Jones

Hello llkooj, welcome to the forum. What is probably happening is the "print udword$(ecx),10,13" line is overwriting the contents of ECX after it runs. Then the next time it is called, it displays this other value. Can you post the entire code you tried? We'll need to see it to help.

If you are using the MASM32 package, study the \masm32\help\asmintro.chm helpfile -- that contains some essential information for programming in assembler. :U Also, if you are looking for assembly tutorials, Google for "Iczelion."
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

llkooj


here is the code. Thanks for your patience. I am such a newbie.

mov I, EBX                      
L$104:                     
mov EBX, I                      
mov ESI, EBX
mov EAX, ESI                  
add EAX, ESI                   
add EAX, ESI                   
add EAX, ESI                   
mov ESI, EAX                   
mov ECX, [A + ESI]                
print udword$(ecx),10,13             
                           
mov ECX, 1                      
mov EDX, EBX                  
add EDX, ECX                  
mov I, EDX                      
                           
mov EDX, 4                      
mov EAX, EBX                  
cmp EAX, EDX                  
jne L$104                  


llkooj

One clarification. What I mean by "Each time the contents of the register changes" - is every time I call the print statement, I have placed a new value in the register and that new value should be printed. However, only the 1st value that was assigned to the register is being printed.

Mark Jones

Hi, comments in the code.


include masm32rt.inc ; Default assemble-time libs.

.data? ; Uninitialized data section
I dd ? ; "I" is the address of one dword (4 bytes)
A dd ? ; So is "A"

; EAX register is 4 bytes, AX is 2 bytes, AH and AL are one byte,
; see ASMINTRO.CHM for complete explanation.

.code
start: ; Entrypoint here

mov I,ecx ; Copy contents of EBX register (4 bytes)
; to the memory offset "I" (4 bytes)

; Note, EBX and ESP should not be modified
; in typical code, so ECX was used instead.

L$104: ; Loop here
mov ecx,I ; Copy contents of address I into ECX                 
mov esi,ecx ; Copy contents of ECX into esi register
mov eax,esi ; Copy contents of eax <-- esi
add eax,esi                 ; Add to EAX whatever is in ESI
add eax,esi ; again
add eax,esi
mov esi,eax ; Copy esi <-- eax
; mov ecx,[A+esi] ; Copy whatever is at the address "A" plus
; the contents of ESI, into register ECX

; Note, if this [A+esi] is outside this
; program, it will trigger a "page fault" err.
; Since this happens with this instruction,
; it has been disabled.

print udword$(ecx),10,13 ; "print" is a macro which expands to a bunch
; of other instructions and calls APIs.
; ANY call may overwrite EAX,ECX, and EDX -
; and this call does exactly that, so our
; ECX value is overwritten, which is why
; initially we get one value, but each
; successive call prints the same thing.

mov ecx,1 ; do more stuff
mov edx,ebx ;
add edx,ecx ;
mov I,edx ;

mov edx,4 ;
mov eax,ebx ;
cmp eax,edx ; compare EDX to EAX
jne L$104 ; "Jump if not equal" to label L$104:

; if EDX equals EAX, then the jump is not
; taken and we continue down here.
invoke ExitProcess,0 ; all code must exit properly.

end start


Here is something that should be fun to tinker with. If you have the MASM32 package installed, you can run the MAKEIT.BAT file in the attached archive to assemble and link it. :U


include masm32rt.inc ; default assemble-time libs

.const
.data?
.data

.code
start:

mov esi,101010 ; some initial values
mov edi,20 ; this will be our "count"

J1: ; loop here

print udword$(esi),13,10,0 ; print the value -- since ESI is not
; overwritten, it will still be the same
; after this print

invoke Sleep,500 ; delay for half a second...

add esi,123456 ; make ESI bigger
dec edi ; decrease edi by one
cmp edi,0 ; is EDI zero?
jnz J1 ; if not, loop!

; when EDI is zero, execution falls through
print "Thanks for looking, have a nice day!",0
invoke Sleep,5000 ; pause 5 seconds here
invoke ExitProcess,0 ; then exit gracefully

end start

[attachment deleted by admin]
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

llkooj


Vortex

Hi llkooj,

You can use also the printf function to display the content of a register :


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib

.data

string      db 'The value of ecx is %d',0

.code

start:

    mov     ecx,1000
    invoke  crt_printf,ADDR string,ecx
    invoke  ExitProcess,0

END start