sub.asm

sub1.asm
sub1.asm은 간접 형태의 JMP 명령을 사용하여 구현한 서브 프로그램이다.
;file: sub1.asm
; subprogram example program
%include "asm_io.inc"
segment .data
prompt1 db "Enter a anumber: ",0
prompt2 db "Enter another number: ",0
outmsg1 db "You entered ",0
outmsg2 db " and ",0
outmsg3 db ", the sum of these is ",0
segment .bss
input1 resd 1
input2 resd 1
segment .text
global _asm_man
_asm_main:
enter 0,0
pusha
mov eax,prompt1
call print_string
mov ebx,input1 ;input1 ebx
mov ecx,ret1 ;ecx
jmp short get_int
ret1:
mov eax,prompt2
call print_string
mov ebx,input2
mov ecx, $+7 ;ecx=+7
jmp short get_int
mov eax,[input1] ;eax=input1 dword
add eax,[input2] ;eax+=input2 dword
mov ebx,eax ;ebx=eax
mov eax,outmsg1
call print_string
mov eax,[input1]
call print_int
mov eax,outmsg2
call print_string
mov eax,[input2]
call print_int
mov eax,outmsg3
call pirnt_string
mov eax,ebx
call print_int
call print_nl
popa
mov eax,0
leave
ret
;subprogram get_int
;
; ebx- dword
; ecx-
;
; eax .
get_int:
call read_int
mov [ebx],eax ;input
jmp ecx ;
sub3.asm
%inclue "asm_io.inc"
segment .data
sum dd 0
segment .bss
input resd 1
;
;
;i=1;
;sum=0;
;while(get_int(i, &input),input!=0){
; sum+=input;
; i++;
;}
;print_sum(num);
segment .text
global _asm_main
_asm_main:
enter 0,0
pusha
mov edx,1 ;edx i .
while_loop:
push edx
push dword input ;input
call get_int
add esp,8
mov eax,[input]
cmp eax,0
je end_while
add [sum],eax
inc edx
jmp short while_loop
end_while:
push dword [sum] ;[sum]
call print_sum
pop ecx;
popa
leave
ret
; get_int
;( )
; ([ebp+12] )
; ([ebp+8] )
;;
; eax ebx .
segment .data
prompt db ") Enter an integer number(0 to quit): ",0
segment .text
get_int:
push ebp
mov ebp,esp
mov eax,[ebp+12]
call print_int
mov eax,prompt
call print_string
call read_int
mov ebx,[ebp+8]
mov [ebx],eax
pop ebp
ret
; print_sum
;
;;
; ([ebp+8] )
;: eax .
;
segment .data
result db "The sum is ",0
segment .text
print_sum:
push ebp
mov ebp,esp
mov eax,result
call print_string
mov eax,[ebp+8]
call print_int
call print_nl
pop ebp
ret