x86-Maschinencode (Linux), 116 Byte
00000000: 89cb 6a02 5931 d289 4c24 f4b0 05cd 8050 ..j.Y1..L$.....P
00000010: 5b53 6a02 5a31 c9b0 13cd 8089 c65b 5331 [Sj.Z1.......[S1
00000020: d231 c9b0 13cd 8089 f75b 53b2 018d 4c24 .1.......[S...L$
00000030: f8b0 04cd 804e 09f6 75ef 5b53 31d2 31c9 .....N..u.[S1.1.
00000040: b013 cd80 89fe 5b53 8d4c 24f4 b201 b003 ......[S.L$.....
00000050: cd80 4e09 f674 0c8a 4424 f838 4424 f474 ..N..t..D$.8D$.t
00000060: e5eb ad89 fe8a 4424 f8c6 4424 f801 3c01 ......D$..D$..<.
00000070: 759e 58c3 u.X.
Nimmt den Dateinamen als Argument
Versammlung (NASM):
section .text
global func
func: ;this function uses fastcall conventions
;seems like no enter instr needed
;open file
mov ebx, ecx ;first argument to func (filename)
push 0x2 ;flag O_RDWR
pop ecx ;pushing a constant is shorter than mov
xor edx, edx ;mode=0
mov [esp-12], ecx ;set first byte (msg) to write to 0. msg later in esp-8, buf in esp-12
mov al, 5 ;using 8 bit regs is smaller
int 0x80 ;syscall open
push eax ;save file descriptor
write_verify:
;get file size
pop ebx ;get fd
push ebx
push 0x2 ;flag SEEK_END
pop edx
xor ecx, ecx ;offset 0
mov al, 0x13
int 0x80 ;syscall lseek
mov esi, eax ;save file byte count in esi
;reset index of file to 0
pop ebx ;get fd
push ebx
xor edx, edx ;flag SEEK_SET=0
xor ecx, ecx ;ecx=0
mov al, 0x13
int 0x80 ;syscall lseek
;***write pass***
mov edi, esi
write_loop: ;for each byte in byte count, write [msg]
;write to file
pop ebx ;file descriptor
push ebx
mov dl, 1 ;bytes to write
lea ecx, [esp-8] ;buffer to write from
mov al, 4
int 0x80 ;syscall write
dec esi ;decrement esi (byte count) to 0
or esi, esi ;cmp esi to 0
jne write_loop ;while esi!=0, keep looping
;reset index of file to 0
pop ebx ;get fd
push ebx
xor edx, edx ;flag SEEK_SET=0
xor ecx, ecx ;ecx=0
mov al, 0x13
int 0x80 ;syscall lseek
;***verification pass***
mov esi, edi
verify_loop: ;for each byte in byte count, verify written byte
pop ebx ;get fd
push ebx
lea ecx, [esp-12] ;buffer to store read byte
mov dl, 1 ;read 1 byte
mov al, 3
int 0x80 ;syscall read
dec esi
or esi, esi ;cmp esi to 0
je end_verify ;at final byte, end verification
mov al, [esp-8]
cmp byte [esp-12],al
je verify_loop ;keep looping if expected value found
jmp write_verify ;if byte!=expected value, restart
end_verify:
mov esi, edi
mov al, [esp-8]
mov byte [esp-8],0x1 ;set new byte to write to 1
cmp al, 0x1
jne write_verify ;if old byte to write!=1, goto start
pop eax ;reset stack
ret
Probieren Sie es online aus! (Verwendet eine temporäre Datei)
-11 Bytes durch Optimieren von sich bewegenden Registern und Verwenden des Stapels als Puffer anstelle einer konstanten Position im Speicher.