[HN Gopher] Show HN: A (marginally) useful x86-64 ELF executable...
___________________________________________________________________
Show HN: A (marginally) useful x86-64 ELF executable in 466 bytes
Author : meribold
Score : 29 points
Date : 2024-03-27 19:52 UTC (3 hours ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| Alifatisk wrote:
| > written in unportable x86 assembly
|
| Why is this a flex?
| msk-lywenn wrote:
| it's not (and I don't think it's supposed to)
| Kluggy wrote:
| So this just reads /sys/class/power_supply/BAT0/energy_now (or
| charge_now) and /sys/class/power_supply/BAT0/charge_full and
| outputs it nicely. While the asm is cool, how large would the
| bash script to do it?
| meribold wrote:
| This project started as a rewrite of a Bash script (for
| learning purposes), so I can easily answer this question. It's
| indeed small. Here's the full Bash script I used to use
| (including comments): #!/usr/bin/env bash
| # Print the remaining amount of energy (or charge) #
| stored in the battery identified by `BAT0`. This # is
| what works on my ThinkPad X220 and may not be #
| portable to other laptops without changes. cd
| /sys/class/power_supply/BAT0 || exit $? #
| Sometimes there are `energy_now` and `energy_full` #
| pseudofiles, and sometimes there are `charge_now` # and
| `charge_full` pseudofiles instead. if [[ -e energy_now
| ]]; then now=$(<energy_now)
| full=$(<energy_full) unit=Wh elif [[ -e
| charge_now ]]; then now=$(<charge_now)
| full=$(<charge_full) unit=Ah fi
| percent=$((100 * now / full)) # Convert from microwatt-
| hours (or microampere-hours) # to watt-hours (or
| ampere-hours). now=$(bc <<< "scale=1; $now / 1000000")
| full=$(bc <<< "scale=1; $full / 1000000") echo "$now
| $unit / $full $unit (${percent}%)"
| meribold wrote:
| This project is a rewrite of a Bash script that I started to
| teach myself a little bit of x86 assembly. I still don't know
| much, but I did learn that ELF executables can be surprisingly
| small and simple for a very basic program. A 64-byte file header
| and a single 56-byte program header are the only "overhead" that
| is really mandatory (and it's even possible to make those overlap
| a little).
|
| But it's difficult to stop the linker from adding extra stuff,
| which is why I eventually specified the headers (and three
| addresses) by hand in the assembly file and stopped using a
| linker.
| fear91 wrote:
| You can shrink it further by doing xorl reg, reg. On x86, the
| upper 32 bits are cleared for you when using 32 bit opcodes. No
| need to do a 64-bit reg, reg xor.
|
| Instead of doing cmp $0, %eax, you can use test eax, eax - that's
| another low hanging fruit.
|
| It seems that you could also preset a dedicated reg to 0 and
| another to 1, further shaving a few bytes.
| meribold wrote:
| Thanks for the suggestions! I'll definitely look into those.
| I'd been hoping posting on HN would result in being able to
| shave off yet a few more bytes.
___________________________________________________________________
(page generated 2024-03-27 23:00 UTC)