From 2aa5b91430b4d9521c53e3095cf94e6b90bab020 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 3 Jan 2019 11:24:51 +0100 Subject: [PATCH] a5: Dokumentation verbessert --- src/c/a5/Makefile | 7 +------ src/c/a5/addsub.asm | 18 +++++++----------- src/c/a5/flagtest.c | 38 ++++++++++++++++++++++++++++++-------- src/c/a5/run.sh | 2 +- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/c/a5/Makefile b/src/c/a5/Makefile index c040c74..cbf7ad2 100644 --- a/src/c/a5/Makefile +++ b/src/c/a5/Makefile @@ -7,14 +7,9 @@ flagtest.o: flagtest.c # ASM-Modul assemblieren addsub.o: addsub.asm - nasm -f elf -o addsub.o addsub.asm + nasm -f elf -o addsub.o addsub.asm # assemblieren # Projekt aufraemen clean: @echo 'Ausgabedateien loeschen' rm -f flagtest *.o *~ - - - - - diff --git a/src/c/a5/addsub.asm b/src/c/a5/addsub.asm index 36d4a5c..524d584 100644 --- a/src/c/a5/addsub.asm +++ b/src/c/a5/addsub.asm @@ -8,15 +8,14 @@ addsub: mov ebp, esp push ebx - push ecx push edx mov eax, 0 - mov bx, word [ebp+16] ;Operation mov ax, word [ebp+8] ;op1 + mov bx, word [ebp+16] ;what - cmp bx, 0x2b + cmp bx, 0x2b;hex code for + symbol jne substract add ax, word [ebp+12] ;op2 @@ -26,21 +25,18 @@ substract: sub ax, word [ebp+12] ;op2 return: - pushfd;Flagregister auf Stack pushen + pushfd;push flags to stack - pop edx;Flagregister in edx schreiben + pop edx;write flags to edx - mov ebx, [ebp+20] + mov ebx, [ebp+20];write flag pointer address to ebx - mov [ebx], edx + mov [ebx], edx;write flags to pointer address pop edx - pop ecx pop ebx mov esp, ebp pop ebp - ret - -section .data \ No newline at end of file + ret \ No newline at end of file diff --git a/src/c/a5/flagtest.c b/src/c/a5/flagtest.c index 8ee030c..6ebf1d7 100644 --- a/src/c/a5/flagtest.c +++ b/src/c/a5/flagtest.c @@ -2,11 +2,10 @@ #include /** - * * @param op1 ebp+8 * @param op2 ebp+12 * @param what ebp+16 - * @param flags ebp+24 + * @param flags ebp+20 * @return */ extern int addsub(int op1, int op2, char what, unsigned int *flags); @@ -26,24 +25,44 @@ int main(int argc, char **argv) { int op2 = atoi(argv[3]); char what = *argv[2]; + + //we need the operators both as signed and unsigned signed short sop1 = (short) op1; signed short sop2 = (short) op2; unsigned short uop1 = (unsigned short) op1; unsigned short uop2 = (unsigned short) op2; + unsigned int *flags = malloc(sizeof(unsigned int)); int result = addsub(op1, op2, what, flags); + //we also need the result as signed and unsigned short signedRes = (short) result; unsigned short unsignedRes = (unsigned short) result; printf("Flags:\n"); printf("O D I T S Z A P C\n"); + + /* + * 0: Overflow flag + * 1: Direction flag + * 2: Interrupt enable flag + * 3: Trap flag + * 4: Signed flag + * 5: Zero flag + * 6: reserved + * 7: Auxiliary flag + * 8: reserved + * 9: Parity flag + * 10: reserved + * 11: Carry flag + */ int flagArray[12]; + //flags are saved as a decimal value, so we need to convert to binary for(int i = 0; i < 12; ++i) { flagArray[i] = *flags % 2; *flags = *flags / 2; @@ -57,21 +76,24 @@ int main(int argc, char **argv) { printf("Ergebnis und Operanden Signed:\n"); printf("%d %c %d = %d\t", sop1, what, sop2, signedRes); - if(flagArray[11] != 0) - printf("Ergebnis ist falsch!"); - else + + if(flagArray[0] == 0) printf("Ergebnis ist richtig!"); + else + printf("Ergebnis ist falsch!"); printf("\n\n"); + printf("Ergebnis und Operanden Unsigned:\n"); printf("%u %c %u = %u\t", uop1, what, uop2, unsignedRes); - if(flagArray[0] != 0) - printf("Ergebnis ist falsch!"); - else + if(flagArray[11] == 0) printf("Ergebnis ist richtig!"); + else + printf("Ergebnis ist falsch!"); + printf("\n\n"); diff --git a/src/c/a5/run.sh b/src/c/a5/run.sh index f4a2b1e..68ca0dd 100755 --- a/src/c/a5/run.sh +++ b/src/c/a5/run.sh @@ -1,4 +1,4 @@ #!/usr/bin/env bash make ./flagtest 32769 + 2 -./flagtest 32769 - 2 +./flagtest 32769 - 2 \ No newline at end of file