Search code examples
cwindowsdebuggingassemblyturbo-c

using windows debugger to view a memory location written to in C?


I have Turbo C and windows debug running in dosbox

I have this C program, it has two main lines, as you can see. int a=5 and then a line to show the address of a, printf("address of a=%x",&a)

enter image description here

I run it

enter image description here

It seems to tell me that a has been allocated the address of fff4

Now I want to use debug to hopefully see the value of 5 at that memory address

But it is not showing

enter image description here

How can I see it in debug?


Solution

  • This is my DEBUG's output of the compiled main function:

    16E1:01FA 55            PUSH    BP                                 
    16E1:01FB 8BEC          MOV BP,SP                              
    16E1:01FD 83EC02        SUB SP,+02                             
    16E1:0200 C746FE0500    MOV WORD PTR [BP-02],0005              
    16E1:0205 8D46FE        LEA AX,[BP-02]                         
    16E1:0208 50            PUSH    AX                                 
    16E1:0209 B89401        MOV AX,0194                            
    16E1:020C 50            PUSH    AX                                 
    16E1:020D E8AA06        CALL    08BA                               
    16E1:0210 59            POP CX                                 
    16E1:0211 59            POP CX                                 
    16E1:0212 8BE5          MOV SP,BP                              
    16E1:0214 5D            POP BP                                 
    16E1:0215 C3            RET
    

    int a=5; is a local variable inside the function main which is stored on the stack (MOV WORD PTR [BP-02],0005). A value on the stack is lost, when you leave the function (RET). You cannot see it outside the running program.

    Your plan can go well, if you

    1. Initialize a global variable and
    2. produce a tiny .com program.

    simplepr.c:

    #include <stdio.h>
    
    int a=5;
    
    void main()
    {
        printf ("address of a=%x",&a);
    }
    

    Compile:

    TCC.EXE -mt -lt simplepr.c
    

    DEBUG session:

    n simplepr.com
    l
    g         -> address of a=125c (example)
    d 125c    -> or what the address is