Simple code:
long long sys_read(unsigned int fd, char* buf, unsigned long count) {
long long ret;
asm volatile (
"movq $0, %%rax\n\t"
"syscall"
: "=a"(ret)
: "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11", "memory"
);
return ret;
}
long long sys_write(unsigned int fd, const char* buf, unsigned long count) {
long long ret;
asm volatile (
"movq $1, %%rax\n\t"
"syscall"
: "=a"(ret)
: "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11", "memory"
);
return ret;
}
void sys_exit(int status) {
asm volatile (
"movq $60, %%rax\n\t"
"syscall"
:
: "D"(status)
);
}
int main() {
char input_buffer[32] = {0};
long long bytes_read = sys_read(0, input_buffer, 31);
int w = 0;
int i = 0;
while (i < bytes_read && input_buffer[i] >= '0' && input_buffer[i] <= '9') {
w = w * 10 + (input_buffer[i] - '0');
i++;
}
if (w > 50) {
w = w - 1;
}
char output_buffer[32];
int len = 0;
if (w == 0) {
output_buffer[len++] = '0';
} else {
char temp[32];
int temp_len = 0;
int temp_w = w;
while (temp_w > 0) {
temp[temp_len++] = (temp_w % 10) + '0';
temp_w /= 10;
}
for (int j = temp_len - 1; j >= 0; j--) {
output_buffer[len++] = temp[j];
}
}
output_buffer[len++] = '\n';
sys_write(1, output_buffer, len);
sys_exit(0);
return 0;
}