Sei sulla pagina 1di 11

CS220

March 26, 2007


More on pointers
.text
.globl len

int len(char * a){ len:


.type len, @function

pushl %ebp
int i=0; movl %esp, %ebp
subl $4, %esp
while(*a++!=0) movl $0, -4(%ebp)
next:
i++; movl 8(%ebp), %eax
incl 8(%ebp)

return i; cmpb $0, (%eax)


je done
incl -4(%ebp)
} jmp next
done:
movl -4(%ebp), %eax
leave
ret
.size len, .-len
Arguments Passing
• Passing by value (make a copy, different
variables)
• Passing by reference (make an alias of a
variable)
• Arguments of C functions are always
passed by value. Pass-by-reference is
achieved in C by explicitly passing pointer
values.
void add2(int *first, int *second){
void add1(int first, int second){ *second+=*first;
second+=first; }
}
main(){
main(){ int a=3;
int a=3; int b=4;
int b=4; add(&a,&b);
add(a,b); printf("a=%d,b=%d\n",a,b);
printf("a=%d,b=%d\n",a,b); }
} .text
.globl add
.text .type add2, @function
.globl add1 add2:
.type add, @function pushl %ebp
add1: movl %esp, %ebp
pushl %ebp movl 12(%ebp), %ecx
movl %esp, %ebp movl 12(%ebp), %edx
movl 8(%ebp), %edx movl 8(%ebp), %eax
leal 12(%ebp), %eax movl (%eax), %eax
addl %edx, (%eax) addl (%edx), %eax
leave movl %eax, (%ecx)
ret leave
.size add, .-add ret
.size add, .-add
Lexicographical ordering
• Alphabetically lower letters are less than
alphabetically higher letters
• Uppercase letters are less than lowercase
letters
• If the shorter string is equal to the same
number of characters in the longer string,
the longer string is greater than the shorter
string.
More string instructions
• cmps[b,w,l]
– compare two strings, advancing in same pace
– Addresses in esi and edi
– (%esi)-(%edi) Contrast: cmp[b,w,l], op2-op1
• scas[b,w,l]
– Scan for a pattern.
– Compares eax/ax/al with the memory space pointed
by edi)
• repe/repne
– repeat until zf=0/1 or ecx=0
Comparing Strings
• The direction flag must be cleared before comparing the
strings.
• Use the CMPSB instruction to compare the strings on a
byte by byte basis in lexicographical order.
• CMPSW or CMPSL instructions do not compare strings
in lexicographical order.
• You must load the ECX register with the length of the
smaller string.
• Use the REPE prefix.
• The ESI and EDI registers must point at the very first
character in the two strings you want to compare.
Repeat string instructions
• REP can be added to the MOVS, LODS,
or STOS (ecx=0)
• REPE/REPZ, REPNE/REPNZ can be
added to CMPS or SCAS (ecx=0 or
ZF=0/1)
String comparison
.section .data
string1: alreadyshort:
.string "test" cld
length1: repe cmpsb
.int 4 je equal
string2: jg greater
.string "test1" less:
length2: movl $-1, %eax
.int 5 jmp done
.section .text greater:
.globl main movl $1, %eax
.type main, @function jmp done
main: equal:
pushl %ebp movl length1, %ecx
movl %esp, %ebp movl length2, %eax
leal string1, %esi cmpl %eax, %ecx
leal string2, %edi ja greater
movl length1, %ecx jb less
movl length2, %eax movl $0, %eax
cmpl %eax, %ecx done:
jb alreadyshort leave str1<str2 return -1
xchg %ecx, %eax ret
str1=str2 return 0
str1>str2 return 1
String Scanning
.section .data
string1: lodsb
.string "This is a test - a long text string to scan." cld
length: repne scasb
.int 44 jne notfound
string2: subl length, %ecx
.string "-" neg %ecx
.section .text decl %ecx
.globl main movl %ecx, %eax
.type main, @function jmp done
main: notfound:
pushl %ebp movl $-1, %eax
movl %esp, %ebp done:
leal string1, %edi leave
leal string2, %esi ret
movl length, %ecx
(Returns index of the first match)
Inline Assembly
• Basic inline
__asm__( assembly code here);
Example:
__asm__( “movb $5, %bl\n\t”
“movb %bl (%eax)“
);

• Extended inline
__asm__( assembly code
: output operands /* optional */
: input operands /* optional */
: list of clobbered registers /* optional */
);
Example:
__asm__( "cld\n\t"
"rep\n\t“
"stosl"
: /* no output registers */
: "c" (count), "a" (fill_value), "D" (dest)
: "%ecx", "%edi"
);

Potrebbero piacerti anche