Skip to content
Sign upLog in
← Back to Community

NASM Print Floating Point

Profile icon
AmazingMech2418

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 [earned 5 cycles]
View Answer
Voters
Profile icon
DevilBullet
Profile icon
ARJPEG
Profile icon
HahaYes
Profile icon
AmazingMech2418
Comments
hotnewtop
Profile icon
Zekka

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

Profile icon
AmazingMech2418

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

Profile icon
DevilBullet

On most modern CPUs, integers and floating point numbers are handled in different ways. Integers are much faster to process in hardware, as there are dedicated circuits for dealing with them quickly. Floating point values have to be dealt with in software, which makes processing them much slower than integers. It's worth noting that the majority of modern programming languages support floats natively so this difference isn't something that programmers have to worry about on a day-to-day basis; however, it can still be important to consider when porting code from one platform to another or when trying to squeeze out the last bit of performance possible from an algorithm.https://printingnearby.com/services-near-by-printing/

Profile icon
CodeLongAndPros

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

Profile icon
VeroNica21

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

Profile icon
AmazingMech2418

@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.

Profile icon
VeroNica21

@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?

Profile icon
VeroNica21

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

Profile icon
xxpertHacker

!) 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.

Profile icon
AmazingMech2418

@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.
Profile icon
CodeLongAndPros

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

Profile icon
xxpertHacker

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

This is ASM, not NASM

Profile icon
CodeLongAndPros

@StudentFires
No, I'm saying ASM != NASM

Profile icon
xxpertHacker

@CodeLongAndPros
That's what I said too!

Profile icon
CodeLongAndPros

@StudentFires

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

Profile icon
xxpertHacker

@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.

Profile icon
HahaYes

ummm is that unicode?

Profile icon
AmazingMech2418

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

Profile icon
HahaYes

@AmazingMech2418
have you tried using long long?

Profile icon
AmazingMech2418

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

Profile icon
HahaYes

@AmazingMech2418
huh, I have no clue now

Profile icon
HahaYes

lol what is going on

Profile icon
AmazingMech2418

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

Profile icon
HahaYes

@AmazingMech2418
me neither

Profile icon
CodeLongAndPros

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

Profile icon
AmazingMech2418

@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.

Profile icon
CodeLongAndPros

@AmazingMech2418

extern printf with NASM?

Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros

@AmazingMech2418
run install-pkg glibc

Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros

@AmazingMech2418
Do you have an ELF entry point?

Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros

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

Profile icon
AmazingMech2418

@CodeLongAndPros
How did you get that?

Profile icon
CodeLongAndPros

@AmazingMech2418
nasm -f bin main.asm

Profile icon
CodeLongAndPros

@AmazingMech2418
Also, it's chmod 777 main

Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros

@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.

Profile icon
CodeLongAndPros

@AmazingMech2418
Wait. Try entry _start

Profile icon
AmazingMech2418

@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.

Profile icon
CodeLongAndPros

@AmazingMech2418

Perhaps the interpreter is the wrong one...
Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros
Profile icon
AmazingMech2418

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

Profile icon
CodeLongAndPros

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

Profile icon
AmazingMech2418

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

Profile icon
CSharpIsGud

@AmazingMech2418
remember that flat binaries can't use extern