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

Fac-simile esonero Sistemi di Calcolo (SC1) A.A. 2017-18


Esercizio 1 (IA32)

Preso da: 18/02/16 C

f.c
void mystrcat(char* a, const char* b) {
    while (*a) a++;
    while (*b) *a++ = *b++;
    *a = 0;
}


main.c
#include <stdio.h>

void mystrcat(char* a, const char* b);

int main() {
    char s1[11] = "Han\0------";
    char s2[17] = "Obi-Wan\0--------";
    char s3[19] = "Anakin Skywalker\0-";

    mystrcat(s1, " Solo");
    printf("mystrcat(s1, \" Solo\")=\"%s\" [corretto=\"Han Solo\" - terminatore %s]\n",
        s1, s1[8]==0 && s1[9]=='-' ? "OK" : "ERR");

    mystrcat(s2, " Kenobi");
    printf("mystrcat(s2, \"Kenobi\")=\"%s\" [corretto=\"Obi-Wan Kenobi\" - terminatore %s]\n",
        s2, s2[14]==0 && s2[15]=='-' ? "OK" : "ERR");

    mystrcat(s3, "");
    printf("mystrcat(s3, \"\")=\"%s\" [corretto=\"Anakin Skywalker\" - terminatore %s]\n",
        s3, s3[16]==0 && s3[17]=='-' ? "OK" : "ERR");

    return 0;
}


Soluzione

f.s
# void mystrcat(char* a, const char* b) {
#    while (*a) a++;
#    while (*b) *a++ = *b++;
#    *a = 0;
# }

.globl mystrcat

mystrcat:
    movl 4(%esp), %eax # a
    movl 8(%esp), %ecx # b
L1: cmpb $0, (%eax)
    je L2
    incl %eax
    jmp L1
L2: cmpb $0, (%ecx)
    je L3
    movb (%ecx), %dl
    movb %dl, (%eax)
    incl %eax
    incl %ecx
    jmp L2
L3: movb $0, (%eax)
    ret


Esercizio 2 (IA32)

Preso da: Esame 13/09/2017 A

f.c
vvoid str_to_upper(const char* a, char* b) {
    int i=0;
    do {
        b[i] = a[i];
        if (b[i] < 'a' || b[i] > 'z') continue; // 'a'==97, 'z'==122
        to_upper(b+i);
    } while (a[i++] != 0);
}


to_upper.s
# void to_lower(char* a) {
#     *a -= 'a' - 'A';
# }

.global to_upper
to_upper:
    pushl %ebx
    movl 8(%esp), %ebx
    movl $0xABADCAFE, %eax
    movl $0xDEADBEEF, %ecx
    movl $0xCAFEBABE, %edx
    subb $32, (%ebx)
    popl %ebx
    ret


main.c
#include <stdio.h>

void str_to_upper(char* a, char* b);

int main() {
    char s1[] = "[Knock@Knock]";
    char c1[sizeof(s1)];
    str_to_upper(s1,c1);
    printf("\"%s\" [corretto: \"[KNOCK@KNOCK]\"]\n", c1);

    char s2[] = "Bazinga!";
    char c2[sizeof(s2)];
    str_to_upper(s2,c2);
    printf("\"%s\" [corretto: \"BAZINGA!\"]\n", c2);

    char s3[] = ">>";
    char c3[sizeof(s3)];
    str_to_upper(s3,c3);
    printf("\"%s\" [corretto: \">>\"]\n", c3);

    char s4[] = "";
    char c4[sizeof(s4)];
    str_to_upper(s4,c4);
    printf("\"%s\" [corretto: \"\"]\n", c4);

    return 0;
}


Soluzione

f.s
# void str_to_upper(const char* a, char* b) {
#     int i=0;
#     do {
#         b[i] = a[i];
#         if (b[i] < 'a' || b[i] > 'z') continue; // 'a'==97, 'z'==122
#         to_upper(b+i);
#     } while (a[i++] != 0);
# }

# void str_to_upper(const char* a, char* b) {  // a <-> esi, b <-> edi
#     int i = 0;                               // i <-> ebx
# L:  char c = a[i];                           // c <-> cl
#     b[i] = c;
#     if (c < 97) goto C;
#     if (c > 122) goto C;
#     to_upper(b+i);
# C:  i++;
#     if (c != 0) goto L;
# }

.globl str_to_upper

str_to_upper:
    pushl %esi
    pushl %edi
    pushl %ebx
    subl $4, %esp
    movl 20(%esp), %esi
    movl 24(%esp), %edi
    xorl %ebx, %ebx             # i = 0
L:  movb (%esi, %ebx), %cl      # c = a[i]
    movb %cl, (%edi, %ebx)      # b[i] = c
    cmpb $97, %cl               # if (c < 97)
    jl C                        #    goto C
    cmpb $122, %cl              # if (c > 122)
    jg C                        #    goto C
    leal (%edi, %ebx), %ecx
    movl %ecx, (%esp)
    call to_upper               # to_upper(b+i)
C:  incl %ebx
    testb %cl, %cl              # if (c != 0)
    jne L                       #    goto L
E:  addl $4, %esp
    popl %ebx
    popl %edi
    popl %esi
    ret


Esercizio 3 (ottimizzazione work)

Preso da: 18/02/16 C

f.c
#include "es4.h"

nodo* get(nodo* p, int i) {
    for (; p != NULL && i--; p = p->next);
    return p;
}

int sum_range(nodo* list, int a, int b) {
    int i, s = 0;
    for (i=a; i<b; i++)
        s += get(list, i)->info;
    return s;
}


es4.h
#ifndef __ES4__
#define __ES4__

#include <stdlib.h>

typedef struct nodo nodo;
struct nodo {
    int info;
    nodo* next;
};

int sum_range(nodo* list, int a, int b);

#endif


main.c
#include <stdio.h>
#include <assert.h>
#include "es4.h"

#define N 100000

nodo* add_to_list(nodo* list, int val) {
    nodo* p = malloc(sizeof(nodo));
    assert(p != NULL);
    p->info = val;
    p->next = list;
    return p;
}

nodo* init(int n) {
    nodo* list = NULL;
    while (n--) list = add_to_list(list, 1);<