Ask coding questions

← Back to all posts
NASM Print Floating Point
h
AmazingMech2418 (899)

So, does anyone know how to print a floating point number in NASM? I'm trying to create a pi approximator for my "pi in nearly every language" series of repls in NASM (by the request of @CodeLongAndPros) and when I try to print a floating point number, it shows random characters (like [email protected] for example) instead of numbers. I already tried using printf using gcc and it said the file didn't exist, so I'm not sure what to do.

Answered by Zekka (40) [earned 5 cycles]
View Answer
Commentshotnewtop
Zekka (40)

Hey! Rather than call out to libc I decided to see if I could hack out just enough machinery to print a float using only the tools you used.

Here's the best I could do! https://repl.it/@Zekka/NASM-Pi-float-printing-demo

AmazingMech2418 (899)

@Zekka Thank you! That's exactly what I needed! I'll give you credit in my pi approximation. Thank you again!

CodeLongAndPros (1354)

Also, I made a template that runs faster than a bash repl.

VeroNica21 (0)

Hai, I have task about regex in python. Can you help me?

AmazingMech2418 (899)

@VeroNica21 First off, I'd recommend opening an ask post on it (especially since Python and NASM are two completely different things). Second, in order to help you, I'd need to know exactly what you need to do and what you need help with.

VeroNica21 (0)

@AmazingMech2418 First, I've opened the ask column. But it seems like there is an error in my account because before I submit it comes out first.
Second, I have an assignment to divide sentences into several words with regex. Example I have the variable txt = "Eid night". In my assignment the first thing to do is to delete whitespace and I've done it here. then I have to divide the sentence in txt with the divisor that I entered like div = 4. so that the output will be ["eidn", "idni", "dnig", "nigh", "ight"]. Can you help me to solve this problem?

VeroNica21 (0)

@AmazingMech2418 oh sorry, i can ask in ask post now. Thank you for your attention

xxpertHacker (388)

!) This is ASM, not NASM,
2) As far as I know, you can't print numbers in ASM, (I may be wrong), but you'd be better off converting it into a string.
Your number is being read as a string, instead of getting a bunch of decimals it's just giving some low ASCII points. Also, your math might be off.

AmazingMech2418 (899)

@StudentFires
1) It is Assembly, but specifically using the NASM assembler, making it NASM. I also am using the NASM syntax as well.
2) The only issue is that I don't know how to convert a float to a string in Assembly. Also, I was just testing the printing of floats, so the math is definitely off because I didn't intend for the same math to be used in the final pi approximator.

CodeLongAndPros (1354)

@StudentFires NASM is not ASM. There are two types of ASM syntax. (AT&T + Intel). GAS uses AT&T. NASM uses Intel.

xxpertHacker (388)

@CodeLongAndPros Why are you telling me that? That's literally what I had said:

This is ASM, not NASM

CodeLongAndPros (1354)

@StudentFires
!) This is ASM, not NASM. Are you saying that your instructions are in ASM, or his code is ASM?

xxpertHacker (388)

@CodeLongAndPros Oh, I forgot that I said that. I didn't look at his stuff too well, looked like normal old ASM at first glance.

HahaYes (1212)

ummm is that unicode?

AmazingMech2418 (899)

@HahaYes I think... I think what it is doing is reading the float as a character.

HahaYes (1212)

@AmazingMech2418 have you tried using long long?

AmazingMech2418 (899)

@HahaYes That doesn't exist in Assembly I don't think.

HahaYes (1212)

lol what is going on

AmazingMech2418 (899)

@HahaYes This is what happens when I try to print a float in NASM... I don't know what to do...

CodeLongAndPros (1354)

Printf needs a double as input for %f, have you tried that?

AmazingMech2418 (899)

@CodeLongAndPros Just using -lc in the ld command or using gcc instead of ld returns a "file not found" error. You need to do one of those to even get the printf function.

AmazingMech2418 (899)

@CodeLongAndPros Well, you have to include libc when linking it and that throws the error.

AmazingMech2418 (899)

@CodeLongAndPros main.sh: line 6: ./main: No such file or directory This is with glibc installed and extern printf in the Assembly file.

AmazingMech2418 (899)

@CodeLongAndPros I'm not sure exactly what you mean. The code is in the linked repl if that helps.

CodeLongAndPros (1354)

@AmazingMech2418 When compiling as a bin:
bash: ./main: cannot execute binary file: Exec format error

AmazingMech2418 (899)

@CodeLongAndPros I don't think bin works on Repl.it. I think only ELF does. I don't really know though...

CodeLongAndPros (1354)

@AmazingMech2418 Repl is a Linux box, which supports Bin.

It means that your code is malformed -badly-.

I think you should save the important bits and rewrite the entry and main stuff. I'm at my wit's end.

AmazingMech2418 (899)

@CodeLongAndPros That doesn't really make sense. This only happens if I include the printf function. If I remove that and remove -lc, it's fine.

CodeLongAndPros (1354)

@AmazingMech2418

Perhaps the interpreter is the wrong one...
AmazingMech2418 (899)

@CodeLongAndPros What do you mean? It should be a compiler anyways...

AmazingMech2418 (899)

@CodeLongAndPros I saw that before. So, what do I do?

CodeLongAndPros (1354)

@AmazingMech2418 I don't know. I've never seen this error.

AmazingMech2418 (899)

@CodeLongAndPros Maybe, is there an alternative to printf for this?

CSharpIsGud (578)

@AmazingMech2418 remember that flat binaries can't use extern