sprintf (mostly) recreated in nasm

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
User avatar
Drew Sebastino
Formerly Espozo
Posts: 3503
Joined: Mon Sep 15, 2014 4:35 pm
Location: Richmond, Virginia

sprintf (mostly) recreated in nasm

Post by Drew Sebastino » Sun Jul 14, 2019 5:38 pm

I got around to re-implementing sprintf in x86-64 assembly using NASM's macro features: https://github.com/DrewSebastino/x86-64 ... printf.asm

Example usage:

Code: Select all

	mov r15, 1000
	sprintf outBuffer, `Value of r15: %#010lX\n%ln`, r15, r10,
	write STDOUT_FILENO, outBuffer, r10

	Value of r15: 0X000003E8
As such, it cannot parse the format string at runtime, although honestly, I have never felt the need to do this. Additionally (and this actually is a problem) there's no floating point support right now, due to having no idea how to implement that sort of thing. If anyone has an idea on what to do for this, I would greatly appreciate it. Everything else (that I know of) is there though.

Posts: 1113
Joined: Fri Feb 24, 2012 12:09 pm

Re: sprintf (mostly) recreated in nasm

Post by nocash » Tue Jul 16, 2019 5:33 am

Runtime parsing would be smaller (if you have a lot of strings (or wasteful if otherwise)).

For floating point, just use the Fxxx FPU opcodes, something like fdiv by powers of 10, then grab the resulting digits as integer... that would be my first idea. Or are you up to something else?

The other functions - wow - you have some pretty surprising optimizations in there, recip(r)o(ri)cal multiplication for displaying decimal numbers, and MMX instructions for hex numbers, I have never ever considered to optimize numeric text output as if it were a cinematic high-end 3D video engine.

PS. Did you thought about implementing GPU and multi-core CPU support for even faster printing ; )

Post Reply