AMD64 machine language on Linux, 51 4644 4441 bytes
0: 6a 00 push 0x0
2: 57 push rdi ; push 1st arg onto stack
3: df 24 24 fbld TBYTE PTR [rsp] ; copy to x87 stack
6: 5f pop rdi
7: 56 push rsi ; push 2nd arg onto stack
8: df 24 24 fbld TBYTE PTR [rsp] ; copy to x87 stack
b: 83 fa 2a cmp edx,0x2a ; multiplication?
e: 74 0e0b je 1e1b <_mul>
10: 83 fa 2b2d cmp edx,0x2b0x2d ; additionsubtraction?
13: 74 0f0a je 241f <_add><_sub>
15: 83 fa 2d 78 0a cmp edx,0x2d
18: 74 08 js 21 <_add> je 22 <_sub>; if the comparison is negative, must ;be subtractionaddition
1a17: de f9 fdivp st(1),st
1c19: eb 08 jmp 2623 <L>
000000000000001e000000000000001b <_mul>:
1e1b: de c9 fmulp st(1),st
201d: eb 04 jmp 2623 <L>
0000000000000022000000000000001f <_sub>:
221f: d9 e0 fchs ; flip sign of value on top of x87 stack (subtraction)
00000000000000240000000000000021 <_add>:
2421: de c1 faddp st(1),st
00000000000000260000000000000023 <L>:
2623: df 34 24 fbstp TBYTE PTR [rsp] ; transfer from x87 stack to integer stack
2926: 58 pop rax ; transfer result to rax
2a27: 5a pop rdx
2b28: c3 ret
Today I learned the x87 floating point unit has hardware support for packed BCD arithmetic. To Try it online!Try it online! compile and run the following C program.
(*f)()="\x6A\x00\x57\xDF\x24\x24\x5F\x56\xDF\x24\x24\x83\xFA\x2A\x74\x0E\x83\xFA\x2B\x74\x0F"="\x6A\x00\x57\xDF\x24\x24\x5F\x56\xDF\x24\x24\x83\xFA\x2A\x74\x0B\x83\xFA\x2D\x74"
"\x83\xFA\x2D\x74\x08\xDE\xF9\xEB\x08\xDE\xC9\xEB\x04\xD9\xE0\xDE\xC1\xDF\x34\x24\x58\x5A\xC3";"\x0A\x78\x0A\xDE\xF9\xEB\x08\xDE\xC9\xEB\x04\xD9\xE0\xDE\xC1\xDF\x34\x24\x58\x5A\xC3";
int main(){
printf("%08x + %08x = %08x\n", 0x00000127, 0x00000003,f(0x00000127, 0x00000003, '+'));
printf("%08x * %08x = %08x\n", 0x00000127, 0x00000003,f(0x00000127, 0x00000003, '*'));
printf("%08x / %08x = %08x\n", 0x00000127, 0x00000003,f(0x00000127, 0x00000003, '/'));
printf("%08x - %08x = %08x\n", 0x00000127, 0x00000003,f(0x00000127, 0x00000003, '-'));
}