Short code to determine compiler's
mcdonald at uxe.cso.uiuc.edu
mcdonald at uxe.cso.uiuc.edu
Sun Jul 16 01:53:00 AEST 1989
>Some students here had to determine the number of registers (data
>and address, we use 680x0's) the C compiler uses. A friend and
>I wrote the following code to show to some students having trouble.
>It is very short and simple, but it seems to work. The only logical
>next step is to post it to comp.lang.c and have it torn apart!
>#include <stdio.h>
>main()
>{
> {
> int count1; /* Base of stackframe */
> register x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,
> x11, x12, x13, x14, x15, x16, x17, x18, x19;
> {
> int count2;
> printf("Number of data registers = %d\n", 19 - abs(&count1 - &count2) + 1);
> }
> }
(code for int * deleted)
Oh my!!!! What on earth makes you think that this will give the
number of register variables actually used? If tried on Microsoft C 5.1
it gives 9 and 9. But, actually Microsoft C uses at most 2 register
variables. Just look at the assembler output of the compiler:
(header removed)
_DATA SEGMENT
$SG180 DB 'Number of data registers = %d', 0aH, 00H
$SG202 DB 'Number of address registers = %d', 0aH, 00H
_DATA ENDS
_TEXT SEGMENT
ASSUME CS: _TEXT
; Line 16
PUBLIC _main
_main PROC NEAR
push bp
mov bp,sp
mov ax,42
call __chkstk
; Line 17
; count1 = -20 (These are the offsets from bp of the local variables)
; x1 = -14
; x2 = -18
; x3 = -24
; x4 = -28
; x5 = -32
; x6 = -36
; x7 = -40
; x8 = -4
; x9 = -8
; x10 = -12
; x11 = -16
; x12 = -22
; x13 = -26
; x14 = -30
; x15 = -34
; x16 = -38
; x17 = -2
; x18 = -6
; x19 = -10
; Line 22
; count2 = -42
; Line 25
mov ax,11
push ax
call _abs
add sp,2
sub ax,20
neg ax
push ax
mov ax,OFFSET DGROUP:$SG180
push ax
call _printf
add sp,4
Nothing in the C language says where variables have to go - putting
an & in front of two different variables (not in an array or struct)
and subtracting the resulting pointers
is a meaningless exercise. Somebody might
assign variables random addresses!!!!! Somebody (i.e. Microsoft) DID!!!!!
If the original writer has "students", that seems to imply that he
is a "teacher". I would hope that anyone teaching C would know this.
In fact, the really interesting question is, in legal C, is it
even POSSIBLE to write a program to see how many registers are used?
Doug McDonald
More information about the Comp.lang.c
mailing list