I am learning from "PC Assembly Language" Paul A. Carter - October 13, 2002.

The calls to print_int for example are calls to interfaced c functions as in this one actually using _printf.

From asking around i got the ideea that eax would be used to return a value or something, but if that isn't true why not use pop eax since i read eax is the fastest reg.

1 %include "asm_io.inc"
2
3 segment .data
4 sum dd 0
5
6 segment .bss
7 input resd 1
8
9 ;
10 ; pseudo-code algorithm
11 ; i = 1;
12 ; sum = 0;
13 ; while( get_int(i, &input), input != 0 ) {
14 ; sum += input;
15 ; i++;
16 ; }
17 ; print_sum(num);
18 segment .text
19 global _asm_main
20 _asm_main:
21 enter 0,0 ; setup routine
22 pusha
23
24 mov edx, 1 ; edx is i in pseudo-code
25 while_loop:
26 push edx ; save i on stack
27 push dword input ; push address on input on stack
28 call get_int
29 add esp, 8 ; remove i and &input from stack
30
31 mov eax,
32 cmp eax, 0
33 je end_while
34
35 add , eax ; sum += input
36
37 inc edx
38 jmp short while_loop
39
40 end_while:
41 push dword ; push value of sum onto stack
42 call print_sum
43 pop ecx ; remove from stack
44
45 popa
46 leave
47 ret
48
49 ; subprogram get_int
50 ; Parameters (in order pushed on stack)
51 ; number of input (at )
52 ; address of word to store input into (at )
53 ; Notes:
54 ; values of eax and ebx are destroyed
55 segment .data
56 prompt db ") Enter an integer number (0 to quit): ", 0
57
58 segment .text
59 get_int:
60 push ebp
61 mov ebp, esp
62
63 mov eax,
64 call print_int
65
66 mov eax, prompt
67 call print_string
68
69 call read_int
70 mov ebx,
71 mov , eax ; store input into memory
72
Posted on 2006-07-31 08:55:58 by w0lfshad3
The "pop ecx" seems like it's there to balance the stack. A "add esp, 4" would have been better, but I guess the guy has size-optimized his code. IMHO that's wrong if this is code that's designed for beginners to learn from.


From asking around i got the ideea that eax would be used to return a value or something, but if that isn't true why not use pop eax since i read eax is the fastest reg.

EAX is indeed generally used to return values. EAX being the "fastest reg" is nonsense, though.
Posted on 2006-07-31 09:04:02 by f0dder
from book:
The compiler would use a POP instead of an ADD because the
ADD requires more bytes for the instruction.


So add ESP, n is faster? Seems that its bigger tough.
Posted on 2006-07-31 09:14:27 by w0lfshad3
pop requires a memory reference, "ADD ESP, n" doesn't. The add is a bit longer yes, but once you need to balance stack for 4 parameters, it gets shorter.

The main point, though, is that "add esp, n" is clearer...
Posted on 2006-07-31 09:30:24 by f0dder

... but if that isn't true why not use pop eax since i read eax is the fastest reg.


The register itself is not faster than others. However some instructions are shorter when they use eax than when they use any other register. So at particular cases there might be speed improvement because of more instructions getting into fetch block.
Posted on 2006-07-31 09:32:27 by arafel
I'll add to that last post ... yeah there's a few 'shortcut' opcodes designed to work with eax (that have smaller binary encodings), but more generally, there are many opcodes which are designed to be used with particular registers, and more often than not, they're designed to work with eax and edx.

For example, the MUL opcode typically acts apon eax such as:
mov eax,250
mov ebx,200
mul ebx ;<--- LOOK eax=eax*ebx

and another example, the DIV opcode also acts on eax, places its result in eax like MUL does, but ALSO any 'remainder' from the division ends up in edx such as:
mov eax,250
mov ebx,200
div ebx ;<-- LOOK eax.edx=eax/ebx
If we performed that, we'd have eax=1 and edx=50

Posted on 2006-08-01 10:59:59 by Homer