Skip to content

Commit

Permalink
Demo Session Laboruebung 4
Browse files Browse the repository at this point in the history
  • Loading branch information
rdrcode committed Jan 24, 2022
1 parent d28d74c commit 85da2d4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ toupper/strtoupper?
lettercount/lettercount64
lettercount/genchartable
lettercount/asciitable.inc

asciitoint/asciitoint
43 changes: 43 additions & 0 deletions asciitoint/Makefile
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)

69 changes: 69 additions & 0 deletions asciitoint/asciitoint.asm
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
32 changes: 32 additions & 0 deletions asciitoint/main.c
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 */

0 comments on commit 85da2d4

Please sign in to comment.