CS360 Lecture notes -- Stack printing programs

  • Jim Plank
  • Directory: /home/plank/cs360/notes/Printstack
  • Lecture notes: http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Printstack/lecture.html
    Sorry, I don't have time to write full-fledged lecture notes for this lecture. I went over two programs, printstack.c and threeprocs.c.

    Here's printstack.c:

    safechar(char c)
    {
      if (c >= 'a' && c <= 'z') return(c);
      if (c >= 'A' && c <= 'Z') return(c);
      if (c >= '0' && c <= '9') return(c);
      if (c == ' ') return(c);
      if (c == '.') return(c);
      if (c == '-') return(c);
      if (c == ',') return(c);
      if (c == '(') return(c);
      if (c == ')') return(c);
      if (c == '[') return(c);
      if (c == ']') return(c);
      if (c == '{') return(c);
      if (c == '}') return(c);
      return '@';
    }
    main(int argc, char **argv, char **envp)
    {
      char **s;
      char *s2;
      int i, top;
    
      if (argc < 2) {
        fprintf(stderr, "usage: printstack nentries (& other junk if you want)\n");
        exit(1);
      }
    
      top = atoi(argv[1]);
    
      printf("&s = 0x%x\n", &s);
      printf("&argc = 0x%x\n", &argc);
      printf("&argv = 0x%x\n", &argv);
      printf("&envp = 0x%x\n", &envp);
      printf("\n");
    
      s = (char **) &s;
      for (i = 0; i < top; i++) {
        s++;
        s2 = (char *) s;
        printf("0x%x : %15d 0x%-8x %2c %2c %2c %2c\n", s, *s, *s, safechar(s2[0]), 
                   safechar(s2[1]), safechar(s2[2]), safechar(s2[3]));
      }
    }
    
    It takes a number as its first argument, and then prints that many words on the stack, starting at the address of s. It prints each word in three ways -- as an int, as a hex value, and as four characters. If a character is not a normal one, then an "at" sign is printed.

    Here is the output on cetus1a, with some values omitted:

    UNIX> printstack 500 Jim Plank
    &s = 0xefffe390
    &argc = 0xefffe3dc
    &argv = 0xefffe3e0
    &envp = 0xefffe3e4
    
    0xefffe394 :               0 0x0         @  @  @  @
    0xefffe398 :               4 0x4         @  @  @  @
    0xefffe39c :      -268442628 0xefffe3fc  @  @  @  @
    0xefffe3a0 :      -268442608 0xefffe410  @  @  @  @
    0xefffe3a4 :          148480 0x24400     @  @  D  @
    0xefffe3a8 :               0 0x0         @  @  @  @
    0xefffe3ac :               0 0x0         @  @  @  @
    0xefffe3b0 :               0 0x0         @  @  @  @
    0xefffe3b4 :               0 0x0         @  @  @  @
    0xefffe3b8 :               0 0x0         @  @  @  @
    0xefffe3bc :               0 0x0         @  @  @  @
    0xefffe3c0 :               0 0x0         @  @  @  @
    0xefffe3c4 :               0 0x0         @  @  @  @
    0xefffe3c8 :               0 0x0         @  @  @  @
    0xefffe3cc :               0 0x0         @  @  @  @
    0xefffe3d0 :               0 0x0         @  @  @  @
    0xefffe3d4 :               0 0x0         @  @  @  @
    0xefffe3d8 :               0 0x0         @  @  @  @
    0xefffe3dc :               4 0x4         @  @  @  @
    0xefffe3e0 :      -268442628 0xefffe3fc  @  @  @  @
    0xefffe3e4 :      -268442608 0xefffe410  @  @  @  @
    0xefffe3e8 :          148480 0x24400     @  @  D  @
    0xefffe3ec :               1 0x1         @  @  @  @
    0xefffe3f0 :               0 0x0         @  @  @  @
    0xefffe3f4 :               0 0x0         @  @  @  @
    0xefffe3f8 :               4 0x4         @  @  @  @
    0xefffe3fc :      -268441864 0xefffe6f8  @  @  @  @
    0xefffe400 :      -268441853 0xefffe703  @  @  @  @
    0xefffe404 :      -268441849 0xefffe707  @  @  @  @
    0xefffe408 :      -268441845 0xefffe70b  @  @  @  @
    0xefffe40c :               0 0x0         @  @  @  @
    0xefffe410 :      -268441839 0xefffe711  @  @  @  @
    
    ...
    
    0xefffe6f0 :               0 0x0         @  @  @  @
    0xefffe6f4 :               0 0x0         @  @  @  @
    0xefffe6f8 :      1886546286 0x7072696e  p  r  i  n
    0xefffe6fc :      1953723489 0x74737461  t  s  t  a
    0xefffe700 :      1667956789 0x636b0035  c  k  @  5
    0xefffe704 :       808452170 0x3030004a  0  0  @  J
    0xefffe708 :      1768751184 0x696d0050  i  m  @  P
    0xefffe70c :      1818324587 0x6c616e6b  l  a  n  k
    0xefffe710 :         4738893 0x484f4d    @  H  O  M
    0xefffe714 :      1161637730 0x453d2f62  E  @  @  b
    
    ....
    
    

    The program threeprocs.c makes two procedure calls and then prints out the top 100 elements of the stack. Again, you should attempt to read the output to see that it all makes sense.

    Here's threeprocs.c:

    a(int j, int *k)
    {
      char **s;
      int i;
    
      s = (char **) &s;
    
      printf("a: &i = 0x%x, &j = 0x%x, &k = 0x%x\n", &i, &j, &k);
    
      for (i = 0; i < 100; i++) {
        s++;
        printf("0x%x : %15d 0x%-8x\n", s, *s, *s);
      }
    }
    
    b(int j)
    {
      int i;
    
      j++;
      i = j+15;
    
      printf("b: &i = 0x%x, &j = 0x%x\n", &i, &j);
    
      a(49, &j);
    }
    
    
    main()
    {
      int i;
    
      i = 333;
    
      b(i);
    }
    
    And here is its output on cetus1a:
    UNIX> threeprocs
    b: &i = 0xefffe2ec, &j = 0xefffe344
    a: &i = 0xefffe26c, &j = 0xefffe2cc, &k = 0xefffe2d0
    0xefffe278 :               0 0x0       
    0xefffe27c :               0 0x0       
    0xefffe280 :               0 0x0       
    0xefffe284 :               0 0x0       
    0xefffe288 :          135664 0x211f0   
    0xefffe28c :      -277080391 0xef7c16b9
    0xefffe290 :      -277086208 0xef7c0000
    0xefffe294 :              32 0x20      
    0xefffe298 :           69832 0x110c8   
    0xefffe29c :      -276896120 0xef7ee688
    0xefffe2a0 :           65684 0x10094   
    0xefffe2a4 :          135368 0x210c8   
    0xefffe2a8 :             333 0x14d     
    0xefffe2ac :               1 0x1       
    0xefffe2b0 :      -277800592 0xef711970
    0xefffe2b4 :           65652 0x10074   
    0xefffe2b8 :               2 0x2       
    0xefffe2bc :      -277800592 0xef711970
    0xefffe2c0 :      -268442880 0xefffe300
    0xefffe2c4 :           67932 0x1095c   
    0xefffe2c8 :      -276895164 0xef7eea44
    0xefffe2cc :              49 0x31      
    0xefffe2d0 :      -268442812 0xefffe344
    0xefffe2d4 :      -268442812 0xefffe344
    0xefffe2d8 :           67476 0x10794   
    0xefffe2dc :           66973 0x1059d   
    0xefffe2e0 :      -278373912 0xef6859e8
    0xefffe2e4 :             900 0x384     
    0xefffe2e8 :             900 0x384     
    0xefffe2ec :             349 0x15d     
    0xefffe2f0 :             192 0xc0      
    0xefffe2f4 :               0 0x0       
    0xefffe2f8 :               0 0x0       
    0xefffe2fc :               0 0x0       
    0xefffe300 :      -277785992 0xef715278
    0xefffe304 :               0 0x0       
    0xefffe308 :               0 0x0       
    0xefffe30c :               0 0x0       
    0xefffe310 :               0 0x0       
    0xefffe314 :               0 0x0       
    0xefffe318 :               0 0x0       
    0xefffe31c :      -277812072 0xef70ec98
    0xefffe320 :               6 0x6       
    0xefffe324 :      -268442660 0xefffe3dc
    0xefffe328 :      -268442632 0xefffe3f8
    0xefffe32c :          135704 0x21218   
    0xefffe330 :               1 0x1       
    0xefffe334 :               0 0x0       
    0xefffe338 :      -268442760 0xefffe378
    0xefffe33c :           67540 0x107d4   
    0xefffe340 :               0 0x0       
    0xefffe344 :             334 0x14e     
    0xefffe348 :               0 0x0       
    0xefffe34c :               0 0x0       
    0xefffe350 :      -268442760 0xefffe378
    0xefffe354 :           67476 0x10794   
    0xefffe358 :               3 0x3       
    0xefffe35c :      -268442660 0xefffe3dc
    0xefffe360 :               4 0x4       
    0xefffe364 :             333 0x14d     
    0xefffe368 :               5 0x5       
    0xefffe36c :      -268442000 0xefffe670
    0xefffe370 :               0 0x0       
    0xefffe374 :               0 0x0       
    0xefffe378 :               6 0x6       
    0xefffe37c :      -268442660 0xefffe3dc
    0xefffe380 :               0 0x0       
    0xefffe384 :               0 0x0       
    0xefffe388 :               0 0x0       
    0xefffe38c :               0 0x0       
    0xefffe390 :               0 0x0       
    0xefffe394 :               0 0x0       
    0xefffe398 :               0 0x0       
    0xefffe39c :               0 0x0       
    0xefffe3a0 :               0 0x0       
    0xefffe3a4 :               0 0x0       
    0xefffe3a8 :               0 0x0       
    0xefffe3ac :               0 0x0       
    0xefffe3b0 :               0 0x0       
    0xefffe3b4 :               0 0x0       
    0xefffe3b8 :               0 0x0       
    0xefffe3bc :               0 0x0       
    0xefffe3c0 :               0 0x0       
    0xefffe3c4 :               0 0x0       
    0xefffe3c8 :               0 0x0       
    0xefffe3cc :               0 0x0       
    0xefffe3d0 :               0 0x0       
    0xefffe3d4 :               0 0x0       
    0xefffe3d8 :               6 0x6       
    0xefffe3dc :      -268441888 0xefffe6e0
    0xefffe3e0 :      -268441877 0xefffe6eb
    0xefffe3e4 :      -268441873 0xefffe6ef
    0xefffe3e8 :      -268441870 0xefffe6f2
    0xefffe3ec :      -268441865 0xefffe6f7
    0xefffe3f0 :      -268441862 0xefffe6fa
    0xefffe3f4 :               0 0x0       
    0xefffe3f8 :      -268441858 0xefffe6fe
    0xefffe3fc :      -268441831 0xefffe719
    0xefffe400 :      -268441446 0xefffe89a
    0xefffe404 :      -268441432 0xefffe8a8