Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==========================================
- #------------------data--------------------
- #==========================================
- .section .data
- msg:
- .ascii "hallo world"
- .set len, .-msg
- RTLWriteIntegerBuffer:
- .byte 0x90,0x8d,0x40,0x00
- .byte 0x90,0x8d,0x40,0x00
- .byte 0x90,0x8d,0x40,0x00
- IsEOF:
- .byte 0
- #==========================================
- #-------------------bss--------------------
- #==========================================
- .bss
- .lcomm ReadCharBuffer, 1
- .lcomm ReadCharInited, 1
- .lcomm ReadCharBytesRead,4
- #==========================================
- #------------------text--------------------
- #==========================================
- .section .text
- .globl _start
- _start:
- jmp zaloop
- #------------------------------------------
- #----------------WriteChar-----------------
- #------------------------------------------
- RTLWriteChar:
- pushq %rbp
- movq %rsp, %rbp #better use base ptr to stack frame
- #instead of stack itself
- movq $1, %rax #syscall №
- movq $1, %rdi #param1, fd
- movq %rsp, %rsi #p2, buf
- addq $16, %rsi #stack:[0ret,8rbp,16arg]
- movq $1, %rdx #p3, count
- syscall
- popq %rbp
- ret
- #------------------------------------------
- #--------------WriteInteger----------------
- #------------------------------------------
- RTLWriteInteger:
- pushq %rbp
- movq %rsp, %rbp
- movq 16(%rbp), %rbx #arg: count
- movq 24(%rbp), %rax #arg: num
- cmpq $0, %rax
- jnl RTLWriteIntegerNotSigned
- negq %rax
- decq %rbx
- pushq $'-' #dont forget to pop
- call RTLWriteChar
- addq $8, %rsp #popped!
- RTLWriteIntegerNotSigned:
- xorq %rcx, %rcx
- pushq %rax
- pushq %rbx
- RTLWriteIntegerPreCheckLoop:
- testq %rax, %rax
- jz RTLWriteIntegerPreCheckLoopDone
- incq %rcx
- movq $10, %rbx
- xorq %rdx, %rdx
- idiv %rbx #suffix here q?
- RTLWriteIntegerPreCheckLoopDone:
- testq %rcx, %rcx
- setz %dl
- orb %dl, %cl
- popq %rbx
- popq %rax
- subq %rcx, %rbx
- cmpq $0, %rbx
- jle RTLWriteIntegerNotPadding
- pushq %rcx
- RTLWriteIntegerPaddingLoop:
- pushq $' ' #pop!
- call RTLWriteChar
- addq $8, %rsp #popped
- decq %rbx
- jnz RTLWriteIntegerPaddingLoop
- popq %rcx
- RTLWriteIntegerNotPadding:
- leaq RTLWriteIntegerBuffer-1(%rcx), %rdi
- #LEA EDI,[OFFSET RTLWriteIntegerBuffer+ECX-1]
- pushq %rcx
- RTLWriteIntegerLoop:
- movq $10, %rsi
- xorq %rdx, %rdx
- idiv %rsi
- leaq '0'(%rdx), %rbx
- movb (%rdi), %bl
- decq %rdi
- loop RTLWriteIntegerLoop
- popq %rcx
- #invoke pop ret
- #------------------------------------------
- #-----------------WriteLn------------------
- #------------------------------------------
- RTLWriteLn:
- pushq %rbp
- movq %rsp, %rbp
- pushq $13
- call RTLWriteChar
- addq $8, %rsp
- pushq $10
- call RTLWriteChar
- addq $8, %rsp
- popq %rbp
- ret
- ReadCharEx:
- pushq %rbp
- movq %rsp, %rbp
- movq $0, %rax
- movq $0, %rdi
- movq $ReadCharBuffer, %rsi
- movq $1, %rdx
- syscall
- movq (ReadCharBuffer), %rax
- /*cmpq $'x', %rax
- je a1
- movq $1, %rax
- movq $1, %rdi
- movq $msg, %rsi
- movq $1, %rdx
- syscall
- jmp end
- a1:
- movq $1, %rax
- movq $1, %rdi
- movq $msg, %rsi
- movq $2, %rdx
- syscall
- end:*/
- popq %rbp
- ret
- ReadCharInit:
- cmpb $0, ReadCharInited
- jnz ReadInitDone
- call ReadCharEx
- movq $1, ReadCharInited
- ReadInitDone:
- ret
- #------------------------------------------
- #----------------ReadChar------------------
- #------------------------------------------
- RTLReadChar:
- movzx ReadCharBuffer, %rax #also dont need. already x64
- call ReadCharEx #do we really need
- #that shit
- ret
- #------------------------------------------
- #--------------ReadInteger-----------------
- #------------------------------------------
- RTLReadInteger:
- call ReadCharInit
- pushq %rax
- pushq %rbx
- pushq %rcx
- pushq %rdx
- pushq %rsi
- pushq %rdi
- pushq %rbp
- movq %rsp, %rbp
- xorq %rax, %rax
- xorq %rbx, %rbx
- leaq 1(%rax,%rbx,1), %rcx
- ReadIntegerSkipWhiteSpace:
- cmpb $0, IsEOF
- jnz ReadIntegerDone
- cmpb $0, (ReadCharBuffer)
- je ReadIntegerSkipWhiteSpaceDone
- cmpb $32, (ReadCharBuffer)
- ja ReadIntegerSkipWhiteSpaceDone
- call ReadCharEx
- jmp ReadIntegerSkipWhiteSpace
- ReadIntegerSkipWhiteSpaceDone:
- cmpb $'-', (ReadCharBuffer)
- jne ReadIntegerNotSigned
- negq %rcx
- call ReadCharEx
- ReadIntegerNotSigned:
- ReadIntegerLoop:
- movzx ReadCharBuffer, %rbx
- cmpb $'0', %bl
- jb ReadIntegerDone
- cmpb $'9', %bl
- ja ReadIntegerDone
- imul $10, %rax
- lea -'0'(%rax,%rbx,1), %rax
- call ReadCharEx
- jmp ReadIntegerLoop
- ReadIntegerDone:
- imul %rcx
- popq %rbp
- popq %rdi
- popq %rsi
- popq %rdx
- popq %rcx
- popq %rbx
- popq %rax
- ret
- #------------------------------------------
- #------------------ReadLn------------------
- #------------------------------------------
- RTLReadLn:
- call ReadCharInit
- cmpb $0, IsEOF
- jne ReadLnDone
- movb ReadCharBuffer, %bl
- cmpb $10, %bl
- je ReadLnDone
- call ReadCharEx
- jmp RTLReadLn
- ReadLnDone:
- ret
- #------------------------------------------
- #-------------------EOF--------------------
- #------------------------------------------
- RTLEOF:
- movzx IsEOF, %rax
- ret
- #------------------------------------------
- #------------------EOLN--------------------
- #------------------------------------------
- RTLEOLN:
- cmpb $10, ReadCharBuffer
- seteb %bl
- ret
- #------------------------------------------
- #------------------Halt--------------------
- #------------------------------------------
- RTLHalt:
- movq $60, %rax
- movq $0, %rdi
- syscall
- zaloop:
- call RTLWriteLn
- call ReadCharEx
- pushq %rax
- call RTLWriteChar
- addq $8, %rsp
- movq $'D', %rax
- pushq %rax
- call RTLWriteChar
- popq %rax #clear stack
- call RTLHalt
Advertisement
Add Comment
Please, Sign In to add comment