2

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?

9
  • 1
    Once your program exits then any temporary changes to memory are effectively gone. You need to debug while your program is running - set a breakpoint in the program and then examine memory while it's halted at the breakpoint. Commented Oct 6, 2015 at 6:01
  • That is virtual address. Its different for each process. Commented Oct 6, 2015 at 6:02
  • @PaulR , rohan, So is it possible to write a C program that writes to RAM in a non temporary way? Commented Oct 6, 2015 at 6:05
  • @barlop: in some embedded environments you might be able to, but in general no - RAM is temporary, and in most modern operating systems it's virtual memory space anyway, so once your process is gone then the addresses and memory contents have no meaning. Commented Oct 6, 2015 at 6:08
  • Can you give us a little bit more details why you want to do this ? What is your goal ? In a real dos 16 bit environment - which you are targetting with turbo c - you could write to memory outside of your process and have this change survive the exit of your program. Commented Oct 6, 2015 at 6:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

thanks, that looks fascinating, i'd like to try it.. I have DOS 6.22 in a VM in virtualbox, and I have debug and my EXE.. How did you get the assembly instructions of the EXE(that you show at the beginning of your answer)?
@barlop. In DEBUG the command 'u'. I used Turbo Debugger to get the address of "_main".
I can't quite get there i.sstatic.net/Q0jDA.png I can't get it outputting a memory location, and if you can include the turbo debugger command to get the address of _main then even better.
@barlop: 'd 1562' like "dump"! 'g' is "go" and '?' is "help" ;-) The explanation with Turbo Debugger is a little bit complicated. Have you installed Turbo Debugger and which version?
I see it , when int a=5, the 5 is there i.imgur.com/X0bdtxh.png and i.imgur.com/PL66kGc.png thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.