a5: Lessbarkeit mit ein paar #defines etc. verbessert

This commit is contained in:
Johannes Theiner 2019-01-05 23:48:36 +01:00
parent ef58a5ef21
commit 4ec15126d9
4 changed files with 45 additions and 41 deletions

View File

@ -10,5 +10,4 @@ set (LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH})
add_subdirectory(src/c/a3) add_subdirectory(src/c/a3)
add_custom_target(a5 COMMAND make WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/c/a5) add_custom_target(a5 COMMAND make WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/c/a5)

View File

@ -1,15 +1,12 @@
# C-Quellcode kompilieren und mit ASM-Modul linken
flagtest: addsub.o flagtest.o flagtest: addsub.o flagtest.o
gcc -m32 -o flagtest flagtest.o addsub.o -Wall -Wextra # linken gcc -m32 -o flagtest flagtest.o addsub.o -Wall -Wextra # linken
flagtest.o: flagtest.c flagtest.o: flagtest.c
gcc -m32 -c -o flagtest.o flagtest.c -Wall -Wextra # compilieren gcc -m32 -c -o flagtest.o flagtest.c -Wall -Wextra # compilieren
# ASM-Modul assemblieren
addsub.o: addsub.asm addsub.o: addsub.asm
nasm -f elf -o addsub.o addsub.asm # assemblieren nasm -f elf -o addsub.o addsub.asm # assemblieren
# Projekt aufraemen
clean: clean:
@echo 'Ausgabedateien loeschen' @echo 'Ausgabedateien loeschen'
rm -f flagtest *.o *~ rm -f flagtest *.o *~

View File

@ -1,6 +1,12 @@
section .text section .text
op1 equ 8
op2 equ 12
what equ 16
flag equ 20
plus equ 0x2b;hex code for + symbol
global addsub global addsub
addsub: addsub:
@ -12,16 +18,16 @@ addsub:
mov eax, 0 mov eax, 0
mov ax, word [ebp+8] ;op1 mov ax, word [ebp+op1]
mov bx, word [ebp+16] ;what mov bx, word [ebp+what]
cmp bx, 0x2b;hex code for + symbol cmp bx, plus
jne substract jne substract
add ax, word [ebp+12] ;op2 add ax, word [ebp+op2]
jmp return jmp return
substract: sub ax, word [ebp+12] ;op2 substract: sub ax, word [ebp+op2]
return: return:
@ -29,7 +35,7 @@ return:
pop edx;write flags to edx pop edx;write flags to edx
mov ebx, [ebp+20];write flag pointer address to ebx mov ebx, [ebp+flag];write flag pointer address to ebx
mov [ebx], edx;write flags to pointer address mov [ebx], edx;write flags to pointer address

View File

@ -1,6 +1,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
/*
* 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
*/
#define flags_length 12
#define flag_overflow 0
#define flag_carry 11
/** /**
* @param op1 ebp+8 * @param op1 ebp+8
* @param op2 ebp+12 * @param op2 ebp+12
@ -15,15 +35,16 @@ int main(int argc, char **argv) {
printf("Wrong number of arguments\n"); printf("Wrong number of arguments\n");
return -1; return -1;
} }
if (*argv[2] != '+' && *argv[2] != '-') {
char what = *argv[2];
if (what != '+' && what != '-') {
printf("Only the operators +/- are supported\n"); printf("Only the operators +/- are supported\n");
return -1; return -1;
} }
int op1 = atoi(argv[1]); int op1 = atoi(argv[1]);
int op2 = atoi(argv[3]); int op2 = atoi(argv[3]);
char what = *argv[2];
//we need the operators both as signed and unsigned //we need the operators both as signed and unsigned
@ -44,43 +65,26 @@ int main(int argc, char **argv) {
printf("Flags:\n"); printf("Flags:\n");
printf("O D I T S Z A P C\n"); printf("O D I T S Z A P C\n");
int flagArray[flags_length];
/*
* 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 //flags are saved as a decimal value, so we need to convert to binary
for(int i = 0; i < 12; ++i) { for (int i = 0; i < flags_length; ++i) {
flagArray[i] = *flags % 2; flagArray[i] = *flags % 2;
*flags = *flags / 2; *flags = *flags / 2;
} }
for(int i = 12; i > 0; --i) { for (int i = flags_length; i > 0; --i) {
printf("%d ", flagArray[i - 1]); printf("%d ", flagArray[i - 1]);
} }
printf("\n\n"); printf("\n\n");
printf("Ergebnis und Operanden Signed:\n"); printf("Ergebnis und Operanden Signed:\n");
printf("%d %c %d = %d\t", sop1, what, sop2, signedRes); printf("%d %c %d = %d\t", sop1, what, sop2, signedRes);
if(flagArray[0] == 0) printf(flagArray[flag_overflow] == 0 ? "Ergebnis ist richtig!" : "Ergebnis ist falsch!");
printf("Ergebnis ist richtig!");
else
printf("Ergebnis ist falsch!");
printf("\n\n"); printf("\n\n");
@ -89,10 +93,8 @@ int main(int argc, char **argv) {
printf("Ergebnis und Operanden Unsigned:\n"); printf("Ergebnis und Operanden Unsigned:\n");
printf("%u %c %u = %u\t", uop1, what, uop2, unsignedRes); printf("%u %c %u = %u\t", uop1, what, uop2, unsignedRes);
if(flagArray[11] == 0)
printf("Ergebnis ist richtig!"); printf(flagArray[flag_carry] == 0 ? "Ergebnis ist richtig!" : "Ergebnis ist falsch!");
else
printf("Ergebnis ist falsch!");
printf("\n\n"); printf("\n\n");