Sistemi di Calcolo

Corso di Laurea in Ingegneria Informatica e Automatica - A.A. 2017-2018

HomePage | Avvisi | Diario lezioni | Programma | Materiale didattico | Esami | Forum | Login

Lunedì 16 e giovedì 19 ottobre 2017 (Lezione 5 - 150 min) - Demetrescu/Coppa


Esempio 1: verifica se due stringhe sono uguali

main.c
#include <stdio.h>

int streq(char* x, char* y);

int main() {
    printf("%d\n", streq("ciao", "ciaooo")); // 0
    printf("%d\n", streq("ciao", "ciao"));   // 1
    printf("%d\n", streq("ciaooo", "ciao")); // 0
    return 0;
}


incr.c
int streq(char* x, char* y) {
    while (*x == *y) {
        if (*x == 0) return 1;
        x++;
        y++;
    }
    return 0;
}


incr.s
# int streq(char* x, char* y) {
#     while (*x == *y) {
#         if (*x == 0) return 1;
#         x++;
#         y++;
#     }
#     return 0;
# }

# int streq(char* x, char* y) {
#     char *c = x, *d = y, a;
#  L: a = *c;
#     if (a != *d) goto E;
#     if (a != 0) goto Q;
#     return 1;
#  Q: c++;
#     d++;
#     goto L;
#  E: return 0;
# }

.globl streq

streq:
   movl 4(%esp), %ecx # char* c = x
   movl 8(%esp), %edx # char* d = y
L: movb (%ecx), %al   # L: a = *c;
   cmpb (%edx), %al   # if (a != *d) goto E;
   jne E
   cmpb $0, %al       # if (a != 0) goto Q;
   jne Q
   movl $1, %eax      # return 1;
   ret
Q: incl %ecx          # Q: c++;
   incl %edx          # d++;
   jmp L              # goto L;
E: movl $0, %eax      # E: return 0;
   ret

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0646 seconds