-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#============================================================================= | ||
# | ||
# Makefile | ||
# | ||
#----------------------------------------------------------------------------- | ||
# | ||
# DHBW Ravensburg - Campus Friedrichshafen | ||
# | ||
# Vorlesung Systemnahe Programmierung / Verteilte Systeme | ||
# | ||
# Author: Ralf Reutemann | ||
# | ||
#============================================================================= | ||
|
||
CC = gcc | ||
LD = ld | ||
NASM = nasm | ||
NASMOPT64 = -g -f elf64 -F dwarf | ||
CFLAGS = -Wall -g -std=gnu11 -O2 -fno-inline-small-functions | ||
|
||
OS := $(shell uname -s) | ||
ARCH := $(shell uname -m) | ||
|
||
TARGETS = asciitoint | ||
|
||
.SECONDARY: main.o | ||
|
||
.PHONY: all | ||
all: $(TARGETS) | ||
|
||
asciitoint : main.o asciitoint.o | ||
$(CC) $(CFLAGS) -o $@ $^ | ||
|
||
%.o : %.asm | ||
$(NASM) $(NASMOPT64) -l $(basename $<).lst -o $@ $< | ||
|
||
%.o : %.c | ||
$(CC) $(CFLAGS) -c -o $@ $< | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f *.o *.lst $(TARGETS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
SECTION .text | ||
|
||
;------------------------------------------------------------------- | ||
; FUNCTION: dec_to_uint | ||
; | ||
; PURPOSE: Convert ASCII string to unsigned integer | ||
; | ||
; PARAMETERS: (via register) | ||
; RDI - pointer to input ASCII string | ||
; RSI - pointer to address of first invalid character | ||
; | ||
; RETURN: RAX - result of conversion, or 0 in case of error | ||
; | ||
;------------------------------------------------------------------- | ||
global dec_to_uint:function | ||
dec_to_uint: | ||
push rbx | ||
push r12 | ||
|
||
xor ecx,ecx | ||
xor edx,edx | ||
xor eax,eax ; holds converted result | ||
mov ebx,10 ; decimal multiplier per input digit | ||
|
||
; skip leading spaces | ||
.skipspace: | ||
mov cl,[rdi] | ||
cmp cl,' ' | ||
jne .checksign | ||
inc rdi | ||
jmp .skipspace | ||
.checksign: | ||
; check +/- sign | ||
xor r12,r12 ; clear sign flag | ||
mov cl,[rdi] | ||
cmp cl,'+' | ||
je .skipsign | ||
cmp cl,'-' | ||
jne .loop | ||
inc r12 | ||
.skipsign: | ||
inc rdi | ||
.loop: | ||
mov cl,[rdi] | ||
test cl,cl | ||
jz .loop_exit | ||
sub cl,'0' | ||
cmp cl,9 | ||
ja .func_exit | ||
mul ebx | ||
test edx,edx | ||
jnz .func_exit | ||
add eax,ecx | ||
jc .func_exit | ||
inc rdi | ||
jmp .loop | ||
.loop_exit: | ||
test r12,r12 ; check sign flag | ||
jz .func_exit | ||
neg rax ; negate result if set | ||
.func_exit: | ||
test rsi,rsi | ||
jz .nullptr | ||
mov [rsi],rdi | ||
.nullptr: | ||
pop r12 | ||
pop rbx | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*=================================================================== | ||
* DHBW Ravensburg - Campus Friedrichshafen | ||
* | ||
* Vorlesung Systemnahe Programmierung (SNP) | ||
* | ||
* main.c - test skeleton for asciitoint | ||
* | ||
* Author: Ralf Reutemann | ||
* Created: 09.01.2022 | ||
* | ||
*===================================================================*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
extern int dec_to_uint(char *s, char **errptr); | ||
|
||
int | ||
main(int argc, char *argv[]) | ||
{ | ||
char *errptr; | ||
|
||
for(int i = 1; i < argc; i++) { | ||
int result = dec_to_uint(argv[i], &errptr); | ||
printf("'%12s' -> %12d %12d '%c'\n", | ||
argv[i], atoi(argv[i]), | ||
result, *errptr); | ||
} | ||
|
||
exit(EXIT_SUCCESS); | ||
} /* end of main */ |