Assembly in Linux (Stack, External Procedure)

来源:互联网 发布:阿城 知乎 编辑:程序博客网 时间:2024/06/11 04:31

Main Program:

call LoadBuffmov eax,edx

Procedure:

LoadBuff:       push eax         ; Save caller's EAX       push ebx         ; Save caller's EBX       push edx         ; Save caller's EDX       mov eax,3        ; Specify sys_read call       mov ebx,0        ; Specify File Descriptor 0: Standard Input       mov ecx,Buff     ; Pass offset of the buffer to read to       mov edx,BUFFLEN  ; Pass number of bytes to read at one pass       int 80h          ; Call sys_read to fill the buffer       mov ebp,eax      ; Save # of bytes read from file for later       xor ecx,ecx      ; Clear buffer pointer ECX to 0       pop edx          ; Restore caller's EDX       pop ebx          ; Restore caller's EBX       pop eax          ; Restore caller's EAX       ret              ; And return to caller


Here the RET exits from the external procedure and return to the next line after call. More than one RET can be used in a procedure

Procedure must:

1. begin with a label

2. must have at least ret

3. may call another proc (*recursion problem)

*every call push 32bit(4byte) to stack, which could hold at most 1MB

step-by-step:

1. push address of next line of code to stack ( address of mov eax,edx)

2. go to procedure label and start running

3. follow the address on the stack and return to where it was interrupted by the call



原创粉丝点击