From f3181636d06af22d4828b4256c2a62d025fccb8a Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 26 Sep 2021 20:40:20 +0200 Subject: cc: cleanup putint and also zeroing print buffer --- miniany/cc.c | 69 +++++++-------------------------------------------------- miniany/test1.c | 3 +++ 2 files changed, 11 insertions(+), 61 deletions(-) diff --git a/miniany/cc.c b/miniany/cc.c index acba6df..6f4f0a8 100644 --- a/miniany/cc.c +++ b/miniany/cc.c @@ -802,9 +802,15 @@ void genPrologue( struct Compiler *compiler ) putstring( "org $1000000" ); putnl( ); putstring( "jmp _start" ); putnl( ); putstring( "putint:" ); putnl( ); - putstring( "mov esi, 0xa" ); putnl( ); + putstring( "mov esi, 0" ); putnl( ); putstring( "lea edi, [putint_string_fmt]" ); putnl( ); - putstring( "add edi, 9" ); putnl( ); + putstring( "itoa_init:" ); putnl( ); + putstring( "mov byte [edi], 0x20" ); putnl( ); + putstring( "inc edi"); putnl( ); + putstring( "inc esi"); putnl( ); + putstring( "cmp esi, 9" ); putnl( ); + putstring( "jne itoa_init" ); putnl( ); + putstring( "inc esi"); putnl( ); putstring( "itoa_loop:" ); putnl( ); putstring( "mov edx, 0" ); putnl( ); putstring( "div esi" ); putnl( ); @@ -815,70 +821,11 @@ void genPrologue( struct Compiler *compiler ) putstring( "jnz itoa_loop" ); putnl( ); putstring( "mov eax, 4" ); putnl( ); putstring( "mov ebx, 1" ); putnl( ); - /* TODO: itoa in assembly (yuck!) and print resulting string */ putstring( "mov ecx, putint_string_fmt" ); putnl( ); - /* TODO: compute length with a assemble time function sizeof or so.. */ putstring( "mov edx, 11" ); putnl( ); putstring( "int 0x80" ); putnl( ); putstring( "ret" ); putnl( ); putstring( "_start:" ); putnl( ); - -/* - -fmt: .string "%s\n" - -.lcomm converted, 10 - - - .section .text - -.global _start - -_start: - - xorl %ecx, %ecx - xorl %edi, %edi - movl $NUMBER, %eax - -itoa_loop: - # Limpa para divisão - xorl %edx, %edx - - movl $10, %esi - - # Agora nós temos o quociente em %eax - # e o resto da divisão em %edx - idiv %esi - - # Convertendo para a representação em ASCII - addl $48, %edx - - pushl %edx - incl %ecx - - xorl $0, %eax - jnz itoa_loop - -rev_loop: - popl %eax - movl %eax, converted(, %edi, 1) - incl %edi - decl %ecx - - cmpl $0, %ecx - jnz rev_loop - - pusha - pushl $converted - pushl $fmt - call printf - addl $8, %esp - popa - - # Exiting - movl $1, %eax - int $0x80 - */ } void genEpilogue( struct Compiler *compiler ) diff --git a/miniany/test1.c b/miniany/test1.c index 707ff16..1bfb8cf 100644 --- a/miniany/test1.c +++ b/miniany/test1.c @@ -2,8 +2,11 @@ int i; int j; +int k; i = 12+25/5-2*3; // 25/5 -> 5, 12+5 -> 17, 2*3 -> 6, 17-6 -> 11 putint i; j = i/3+3*4; // 11 / 3 -> 3, 3*4 -> 12, 3+12 -> 15 putint j; +k = 0; +putint k; -- cgit v1.2.3-54-g00ecf