Soluzioni esercitazione 3 discusse in classe (Canale AO)
Esercizio 2
void cancspazi(char* s) {
int i=0, j=0;
while (s[i]!=0) {
if (s[i]!=32) {
s[j] = s[i];
j++;
}
i++;
}
s[j] = 0;
}
# void cancspazi(char* s) { // eax <-> s
# int i=0, j=0; // ecx <-> i, %edx <-> j
# L: if (s[i]==0) goto E;
# if (s[i]==32) goto S;
# s[j] = s[i];
# j++;
# S: i++;
# goto L;
# E: s[j] = 0;
# return;
# }
.globl cancspazi
cancspazi:
pushl %ebx # prologo
movl 8(%esp), %eax
xorl %ecx, %ecx # i=0
xorl %edx, %edx # j=0
L: cmpb $0,(%eax,%ecx) # tmp = s[i]-0
je E # if (tmp==0) goto E
cmpb $32,(%eax,%ecx) # tmp = s[i]-32
je S # if (tmp==0) goto S
movb (%eax,%ecx), %bl # b = s[i]
movb %bl, (%eax,%edx) # s[j] = b;
incl %edx # j++;
S: incl %ecx # S: i++;
jmp L # goto L;
E: movb $0, (%eax,%edx) # E: s[j] = 0;
popl %ebx # epilogo
ret
Esercizio 3
# int makepass(int* v, int n) { // esi <-> v, edi <-> n
# int cont = 0; ebx <-> cont
# L: n--;
# if (n<=0) goto E;
# if (v[n]>=v[n-1]) goto L;
# swap(v+n, v+n-1);
# cont = 1;
# goto L;
# E: return cont;
# }
.globl makepass
makepass:
pushl %esi
pushl %edi
pushl %ebx
subl $8, %esp # prologo
movl 24(%esp), %esi # v
movl 28(%esp), %edi # n
xorl %ebx, %ebx # int cont = 0; ebx <-> cont
L: decl %edi # L: n--
cmpl $0, %edi # tmp = n-0
jle E # if (tmp<=0) goto E
movl (%esi,%edi,4), %ecx # c = v[n]
cmpl -4(%esi,%edi,4), %ecx # tmp = c-v[n-1]
jge L # if (tmp>=0) goto L;
leal (%esi,%edi,4), %ecx # c = &v[n]
movl %ecx, (%esp) # 1mo param
leal -4(%esi,%edi,4), %ecx # c = &v[n-1]
movl %ecx, 4(%esp) # 2do param
call swap # swap(v+n, v+n-1);
movl $1, %ebx # cont = 1;
jmp L # goto L;
E: movl %ebx, %eax # E: return cont;
addl $8, %esp # epilogo
popl %ebx
popl %edi
popl %esi
ret