It might be worth looking at how a Microsoft C compiler does it. Note that I could have done this with a 16-bit compiler, but the assembly code would have been longer and harder to understand.
//=============================================================================
#include <windows.h>
#include <conio.h>
#include <stdio.h>
//=============================================================================
int array[4][4];
int main( void )
{
int i,j;
for ( i=0; i < 4; i++ ) // line 13
for ( j=0; j < 4; j++ ) // line 14
array [i][j] = i<<4 | j; // line 15
for ( i=0; i < 4; i++ )
for ( j=0; j < 4; j++ )
printf("%02x ",array [i][j]);
printf("\n");
getch();
}
; Listing generated by Microsoft (R) Optimizing Compiler Version 13.10.3077
TITLE test.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
INCLUDELIB LIBC
INCLUDELIB OLDNAMES
_DATA SEGMENT
COMM _array:DWORD:010H
$SG74403 DB '%02x ', 00H
ORG $+2
$SG74404 DB 0aH, 00H
_DATA ENDS
PUBLIC _main
EXTRN _getch:NEAR
EXTRN _printf:NEAR
; Function compile flags: /Odt
_TEXT SEGMENT
_j$ = -8 ; size = 4
_i$ = -4 ; size = 4
_main PROC NEAR
; File c:\program files\microsoft visual c++ toolkit 2003\my\array\test.c
; Line 10
push ebp
mov ebp, esp
sub esp, 8
; Line 13
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L74391
$L74392:
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
$L74391:
cmp DWORD PTR _i$[ebp], 4
jge SHORT $L74393
; Line 14
mov DWORD PTR _j$[ebp], 0
jmp SHORT $L74394
$L74395:
mov ecx, DWORD PTR _j$[ebp]
add ecx, 1
mov DWORD PTR _j$[ebp], ecx
$L74394:
cmp DWORD PTR _j$[ebp], 4
jge SHORT $L74396
; Line 15
mov edx, DWORD PTR _i$[ebp]
shl edx, 4
or edx, DWORD PTR _j$[ebp]
mov eax, DWORD PTR _i$[ebp]
shl eax, 4
mov ecx, DWORD PTR _j$[ebp]
mov DWORD PTR _array[eax+ecx*4], edx
jmp SHORT $L74395
$L74396:
jmp SHORT $L74392
$L74393:
; Line 17
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L74397
$L74398:
mov edx, DWORD PTR _i$[ebp]
add edx, 1
mov DWORD PTR _i$[ebp], edx
$L74397:
cmp DWORD PTR _i$[ebp], 4
jge SHORT $L74399
; Line 18
mov DWORD PTR _j$[ebp], 0
jmp SHORT $L74400
$L74401:
mov eax, DWORD PTR _j$[ebp]
add eax, 1
mov DWORD PTR _j$[ebp], eax
$L74400:
cmp DWORD PTR _j$[ebp], 4
jge SHORT $L74402
; Line 19
mov ecx, DWORD PTR _i$[ebp]
shl ecx, 4
mov edx, DWORD PTR _j$[ebp]
mov eax, DWORD PTR _array[ecx+edx*4]
push eax
push OFFSET FLAT:$SG74403
call _printf
add esp, 8
jmp SHORT $L74401
$L74402:
jmp SHORT $L74398
$L74399:
; Line 20
push OFFSET FLAT:$SG74404
call _printf
add esp, 4
; Line 22
call _getch
; Line 23
xor eax, eax
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END