array1.asm

array1.asm
%define ARRAY_SIZE 100
%define NEW_LINE 10
segment .data
FirstMsg db "First 10 elements of array",0
Prompt db "Enter index of element to display: ",0
SecondMsg db "Element %d is %d", NEW_LINE,0
ThirdMsg db "Elemetns 20 through 29 of array",0
InputFOrmat db "%d",0
segment .bss
array resd ARRAY_SIZE
segment .text
extern _puts, _printf, _scanf, _dump_line
global _asm_main
global_main:
enter 4,0
push ebx
push esi
; 100,99,98,97, ...
mov ecx, ARRAY_SIZE
mov ebx, array
init_loop:
mov [ebx], ecx
add ebx, 4
loop init_loop
push dword FirstMsg ;FirstMsg
call _puts
pop ecx
push dword 10
push dword array
call _print_array
add esp, 8
; prompt user for element index
Prompt_loop:
push dword Prompt
call _printf
pop ecx
lea eax, [ebp-4] ;eax=
push eax
push dword InputFormat
call _scanf
add esp, 8
cmp eax, 1 ;eax=scanf
je InputOK
call _dump_line ;
jmp Prompt_loop ;
InputOK:
mov esi, [ebp-4]
push dword [array+4*esi]
push esi
push dword SecondMsg ;
call _printf
add esp, 12
push dword ThirdMsg ; 20-29
call _puts
pop ecx
push dword 10
push dword array+20*4 ;array[20]
call _print_array
add esp, 8
pop esi
pop ebx
mov eax, 0
leave
ret
;
; _print_array
; C , .
; C :
; void print_array(const int * a, int n);
; :
; a- ( ebp+8 )
; n- ( ebp+12)
segment .data
OutputFormat db "%-5d %d", NEW_LINE,0
segment .text
global _print_array
_print_array:
enter 0,0
push esi
push ebx
xor esi, esi
mov ecx, [ebp+12]
mov ebx, [ebp+8]
print_loop:
push ecx
push dword [ebx+4*esi]
push esi
push dword OutputFormat
call _printf
add esp, 12
inc esi
pop ecx
loop print_loop
pop ebx
pop esi
leave
ret
array1c.c
#include <stdio.h>
int asm_main(void);
void dump_line(void);
int main()
{
int ret_status;
ret_status=asm_main();
return ret_status;
}
/*
* dump_line
* (char) .
*/
void dump_line()
{
int ch;
while((ch=getchar())!=EOF && ch!='\n'){
/* NULL BODY */
}
}