---
# System prepended metadata

title: CAOS Exercise Session Lecture 4

---

# CAOS Exercise Session Lecture 4

![](https://i.imgur.com/lxZnKu5.png)
![](https://i.imgur.com/bODY4w2.png)
![](https://i.imgur.com/XwtmckS.png)

``` c
long lt_cnt = 0;
long ge_cnt = 0;

long absdiff_se(long x, long y) {
    long result;
    if(x < y) {
        lt_cnt++;
        result = y - x;
    } else {
        ge_cnt++;
        result = x - y;
    }
    return result;
}

long absdiff_se2(long x, long y) {
    long result;
    int t = x < y;
    if(t)
        goto true;
    ge_cnt++;
    result = x - y;  
    goto done;
true:
    lt_cnt++;
    result = y - x;
done:
    return result;
}
```


![](https://i.imgur.com/4MAxm7D.png)
![](https://i.imgur.com/kwquD0o.png)

>fejl i opgave: C-koden bør anvende longs, ikke shorts
                       Istedet for %rbx, skal %rax benyttes
fejl i løsning: se evt mit forslag istedet for bogens.
Fejl i bogens løsning: (sidste betingelse skal være y>=10)
(bemærk også at idivq instruktionen kun findes som enkelt-operand instruktion, se afsnit CPP 3.5.5, men ignorer dette for nuværende)

3

```c
long test(long x, long y) {
    long val = 12 + y;
    if (x == 0) {
        if (y == 10)
            val = x;
        else
            val = 0;
    }
    else if (-----)
        val = -----;
    return val;
}
```

rbx = val = 12 * y = 12 * rsi
if(x == 0) {
    y / x = y / 0 = 0     y = 0
    10 - y = 10 - 0
    val = x = 0
} else {
    
}



![](https://i.imgur.com/Lm1WTgI.png)
![](https://i.imgur.com/d585Zx3.png)


| Array | Element size | Total size | Start address | Element $i$ |
|-------|--------------|------------|---------------|-----------|
| P     |   4 bytes|  20 bytes |  $x_P$ | $(x_P, i, 4)$ |
| Q     | 2 bytes  | 4 bytes |  $x_Q$ | $(x_Q, i, 2)$ |
| R     | 4 bytes | 52 bytes |  $x_R$ | $(x_R, i, 4)$ |
| S     | 8 bytes | 88 bytes |  $x_S$ | $(x_S, i, 8)$ |
| T     | 2 bytes  | 12 bytes |  $x_T$ |  |

![](https://i.imgur.com/VeBLXLa.png)
