.file "fasit.c" | |||||
.text | |||||
.globl writebyte | |||||
.type writebyte, @function | |||||
writebyte: | |||||
.LFB5: | |||||
.cfi_startproc | |||||
pushl %ebp | |||||
.cfi_def_cfa_offset 8 | |||||
.cfi_offset 5, -8 | |||||
movl %esp, %ebp | |||||
.cfi_def_cfa_register 5 | |||||
subl $24, %esp | |||||
movl 12(%ebp), %eax | |||||
movb %al, -12(%ebp) | |||||
pushl 8(%ebp) | |||||
pushl $1 | |||||
pushl $1 | |||||
leal -12(%ebp), %eax | |||||
pushl %eax | |||||
call fwrite | |||||
addl $16, %esp | |||||
nop | |||||
leave | |||||
.cfi_restore 5 | |||||
.cfi_def_cfa 4, 4 | |||||
ret | |||||
.cfi_endproc | |||||
.LFE5: | |||||
.size writebyte, .-writebyte | |||||
.globl writeutf8char | |||||
.type writeutf8char, @function | |||||
writeutf8char: | |||||
.LFB6: | |||||
.cfi_startproc | |||||
pushl %ebp | |||||
.cfi_def_cfa_offset 8 | |||||
.cfi_offset 5, -8 | |||||
movl %esp, %ebp | |||||
.cfi_def_cfa_register 5 | |||||
subl $24, %esp | |||||
cmpl $127, 12(%ebp) | |||||
ja .L3 | |||||
movl 12(%ebp), %eax | |||||
movzbl %al, %eax | |||||
subl $8, %esp | |||||
pushl %eax | |||||
pushl 8(%ebp) | |||||
call writebyte | |||||
addl $16, %esp | |||||
jmp .L2 | |||||
.L3: | |||||
cmpl $65535, 12(%ebp) | |||||
jbe .L5 | |||||
movl $18, -12(%ebp) | |||||
movl -12(%ebp), %eax | |||||
movl 12(%ebp), %edx | |||||
movl %eax, %ecx | |||||
shrl %cl, %edx | |||||
movl %edx, %eax | |||||
andl $7, %eax | |||||
orl $-16, %eax | |||||
movzbl %al, %eax | |||||
subl $8, %esp | |||||
pushl %eax | |||||
pushl 8(%ebp) | |||||
call writebyte | |||||
addl $16, %esp | |||||
jmp .L6 | |||||
.L5: | |||||
cmpl $2047, 12(%ebp) | |||||
jbe .L7 | |||||
movl $12, -12(%ebp) | |||||
movl -12(%ebp), %eax | |||||
movl 12(%ebp), %edx | |||||
movl %eax, %ecx | |||||
shrl %cl, %edx | |||||
movl %edx, %eax | |||||
andl $15, %eax | |||||
orl $-32, %eax | |||||
movzbl %al, %eax | |||||
subl $8, %esp | |||||
pushl %eax | |||||
pushl 8(%ebp) | |||||
call writebyte | |||||
addl $16, %esp | |||||
jmp .L6 | |||||
.L7: | |||||
movl $6, -12(%ebp) | |||||
movl -12(%ebp), %eax | |||||
movl 12(%ebp), %edx | |||||
movl %eax, %ecx | |||||
shrl %cl, %edx | |||||
movl %edx, %eax | |||||
andl $31, %eax | |||||
orl $-64, %eax | |||||
movzbl %al, %eax | |||||
subl $8, %esp | |||||
pushl %eax | |||||
pushl 8(%ebp) | |||||
call writebyte | |||||
addl $16, %esp | |||||
.L6: | |||||
subl $6, -12(%ebp) | |||||
movl -12(%ebp), %eax | |||||
movl 12(%ebp), %edx | |||||
movl %eax, %ecx | |||||
shrl %cl, %edx | |||||
movl %edx, %eax | |||||
andl $63, %eax | |||||
orl $-128, %eax | |||||
movzbl %al, %eax | |||||
subl $8, %esp | |||||
pushl %eax | |||||
pushl 8(%ebp) | |||||
call writebyte | |||||
addl $16, %esp | |||||
cmpl $0, -12(%ebp) | |||||
jg .L6 | |||||
.L2: | |||||
leave | |||||
.cfi_restore 5 | |||||
.cfi_def_cfa 4, 4 | |||||
ret | |||||
.cfi_endproc | |||||
.LFE6: | |||||
.size writeutf8char, .-writeutf8char | |||||
.globl readbyte | |||||
.type readbyte, @function | |||||
readbyte: | |||||
.LFB7: | |||||
.cfi_startproc | |||||
pushl %ebp | |||||
.cfi_def_cfa_offset 8 | |||||
.cfi_offset 5, -8 | |||||
movl %esp, %ebp | |||||
.cfi_def_cfa_register 5 | |||||
subl $24, %esp | |||||
pushl 8(%ebp) | |||||
pushl $1 | |||||
pushl $1 | |||||
leal -13(%ebp), %eax | |||||
pushl %eax | |||||
call fread | |||||
addl $16, %esp | |||||
movl %eax, -12(%ebp) | |||||
cmpl $0, -12(%ebp) | |||||
jg .L9 | |||||
movl $-1, %eax | |||||
jmp .L11 | |||||
.L9: | |||||
movzbl -13(%ebp), %eax | |||||
movzbl %al, %eax | |||||
.L11: | |||||
leave | |||||
.cfi_restore 5 | |||||
.cfi_def_cfa 4, 4 | |||||
ret | |||||
.cfi_endproc | |||||
.LFE7: | |||||
.size readbyte, .-readbyte | |||||
.globl readutf8char | |||||
.type readutf8char, @function | |||||
readutf8char: | |||||
.LFB8: | |||||
.cfi_startproc | |||||
pushl %ebp | |||||
.cfi_def_cfa_offset 8 | |||||
.cfi_offset 5, -8 | |||||
movl %esp, %ebp | |||||
.cfi_def_cfa_register 5 | |||||
subl $8, %esp | |||||
subl $12, %esp | |||||
pushl 8(%ebp) | |||||
call readbyte | |||||
addl $16, %esp | |||||
leave | |||||
.cfi_restore 5 | |||||
.cfi_def_cfa 4, 4 | |||||
ret | |||||
.cfi_endproc | |||||
.LFE8: | |||||
.size readutf8char, .-readutf8char | |||||
.ident "GCC: (GNU) 6.3.1 20170306" | |||||
.section .note.GNU-stack,"",@progbits |
.extern fread, fwrite | |||||
.text | |||||
.globl readbyte | |||||
# Navn: readbyte | |||||
# Synopsis: Leser en byte fra en binærfil. | |||||
# C-signatur: int readbyte (FILE *f) | |||||
# Registre: | |||||
readbyte: | |||||
pushl %ebp # Standard funksjonsstart | |||||
movl %esp,%ebp # | |||||
rb_x: popl %ebp # Standard | |||||
ret # retur. | |||||
.globl readutf8char | |||||
# Navn: readutf8char | |||||
# Synopsis: Leser et Unicode-tegn fra en binærfil. | |||||
# C-signatur: long readutf8char (FILE *f) | |||||
# Registre: | |||||
readutf8char: | |||||
pushl %ebp # Standard funksjonsstart | |||||
movl %esp,%ebp # | |||||
popl %ebp # Standard | |||||
ret # retur. | |||||
.globl writebyte | |||||
# Navn: writebyte | |||||
# Synopsis: Skriver en byte til en binærfil. | |||||
# C-signatur: void writebyte (FILE *f, unsigned char b) | |||||
# Registre: | |||||
writebyte: | |||||
pushl %ebp # Standard funksjonsstart | |||||
movl %esp,%ebp # | |||||
popl %ebp # Standard | |||||
ret # retur. | |||||
.globl writeutf8char | |||||
# Navn: writeutf8char | |||||
# Synopsis: Skriver et tegn kodet som UTF-8 til en binærfil. | |||||
# C-signatur: void writeutf8char (FILE *f, unsigned long u) | |||||
# Registre: | |||||
writeutf8char: | |||||
pushl %ebp # Standard funksjonsstart | |||||
movl %esp,%ebp # | |||||
wu8_x: popl %ebp # Standard | |||||
ret # retur. |
#include <stdio.h> | |||||
#include <stdlib.h> | |||||
typedef unsigned char byte; | |||||
typedef unsigned long unicode; | |||||
// The minimum value of the first `char` to indicate n bytes | |||||
#define C4bytes 0xF0 | |||||
#define C3bytes 0xE0 | |||||
#define C2bytes 0xC0 | |||||
// The minimum value for the vm_var_char for the utf8 equivalent to be n bytes | |||||
#define U4bytes 0x10000 | |||||
#define U3bytes 0x0800 | |||||
#define U2bytes 0x0080 | |||||
void writebyte(FILE *f, byte b) | |||||
{ | |||||
fwrite(&b, 1, 1, f); | |||||
} | |||||
void writeutf8char(FILE *f, unicode u) | |||||
{ | |||||
if (u >= U4bytes) | |||||
{ | |||||
writebyte(f, 0b11110000 | ((u >> 18) & 0b00000111)); | |||||
writebyte(f, 0b10000000 | ((u >> 12) & 0b00111111)); | |||||
writebyte(f, 0b10000000 | ((u >> 6) & 0b00111111)); | |||||
writebyte(f, 0b10000000 | (u & 0b00111111)); | |||||
} | |||||
else if (u >= U3bytes) | |||||
{ | |||||
writebyte(f, 0b11100000 | ((u >> 12) & 0b00001111)); | |||||
writebyte(f, 0b10000000 | ((u >> 6) & 0b00111111)); | |||||
writebyte(f, 0b10000000 | (u & 0b00111111)); | |||||
} | |||||
else if (u >= U2bytes) | |||||
{ | |||||
writebyte(f, 0b11000000 | ((u >> 6) & 0b00011111)); | |||||
writebyte(f, 0b10000000 | (u & 0b00111111)); | |||||
} | |||||
else | |||||
{ | |||||
writebyte(f, u); | |||||
} | |||||
} | |||||
int readbyte(FILE *f) | |||||
{ | |||||
int status; | |||||
byte c; | |||||
status = fread(&c, 1, 1, f); | |||||
if (status <= 0) return -1; | |||||
return (int)c; | |||||
} | |||||
long readutf8char(FILE *f) | |||||
{ | |||||
byte first = readbyte(f); | |||||
unicode u = 0; | |||||
if (first >= C4bytes) | |||||
{ | |||||
u |= (first & 0b00000111) << 18; | |||||
u |= (readbyte(f) & 0b00111111) << 12; | |||||
u |= (readbyte(f) & 0b00111111) << 6; | |||||
u |= readbyte(f) & 0b00111111; | |||||
} | |||||
else if (first >= C3bytes) | |||||
{ | |||||
u |= (first & 0b00001111) << 12; | |||||
u |= (readbyte(f) & 0b00111111) << 6; | |||||
u |= readbyte(f) & 0b00111111; | |||||
} | |||||
else if (first >= C2bytes) | |||||
{ | |||||
u |= (first & 0b00011111) << 6; | |||||
u |= readbyte(f) & 0b00111111; | |||||
} | |||||
else | |||||
{ | |||||
u |= first; | |||||
} | |||||
return u; | |||||
} |