Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot print 32 bit integers #28

Closed
crclark96 opened this issue Feb 17, 2021 · 1 comment · Fixed by #32
Closed

Cannot print 32 bit integers #28

crclark96 opened this issue Feb 17, 2021 · 1 comment · Fixed by #32

Comments

@crclark96
Copy link

When I try to print a u32 variable above 65535 (u16::MAX), the value wraps to a lower value below 65535. This also occurs with i32 overflowing at i16::MAX.

I made a reproduction here: https://github.com/crclark96/ufmt-i32-print-repro, but I'll copy main source file and output below:

#![no_std]
#![no_main]

extern crate panic_halt;

use arduino_uno::prelude::*;

#[arduino_uno::entry]
fn main() -> ! {
    let dp = arduino_uno::Peripherals::take().unwrap();

    let mut pins = arduino_uno::Pins::new(
        dp.PORTB,
        dp.PORTC,
        dp.PORTD,
    );
    let mut serial = arduino_uno::Serial::new(
        dp.USART0,
        pins.d0,
        pins.d1.into_output(&mut pins.ddr),
        57600.into_baudrate(),
    );

    ufmt::uwriteln!(&mut serial, "unsigned 0..80_000\r").void_unwrap();

    for x in (0u32..80_000u32).step_by(10000) {
        ufmt::uwriteln!(&mut serial, "{}\r", x).void_unwrap();
    }

    ufmt::uwriteln!(&mut serial, "signed 0..80_000\r").void_unwrap();

    for x in (0i32..80_000i32).step_by(10000) {
        ufmt::uwriteln!(&mut serial, "{}\r", x).void_unwrap();
    }

    loop {}
}

Output:

unsigned 0..80_000
0
10000
20000
30000
40000
50000
60000
4464
signed 0..80_000
0
10000
20000
30000
-25536
-15536
-5536
4464
@Serentty
Copy link

I got the same issue writing code for the 6502. I think I've figured out the problem, and it's that the implementation for printing 32-bit integers casts them to a usize first. I have no idea why it would be implemented in such a non-portable way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants