Jump added

This commit is contained in:
2019-03-15 19:54:33 +01:00
parent 96391dd93c
commit 6a037ea2ec
2 changed files with 152 additions and 0 deletions

34
x86_64/3_homework.asm Normal file
View File

@@ -0,0 +1,34 @@
global _start
; Program that finds the highest number in the byte array declared below
section .data
data: db 0, 15, 57, 2, 43, 87, 2
dataSize: equ 7
section .bss
section .text
_start:
xor rbx, rbx
xor rcx, rcx
mov rax, 1 ; iterator value
mov cl, byte [data] ; default value for the result
loop_start:
cmp rax, dataSize
jge loop_end
mov bl, byte [rax + data] ; load next candidate
cmp cl, bl ; compare to (default) value
jge loop_inc ; skip if cl >= bl
mov cl, bl ; set higher value
loop_inc:
inc rax
jmp loop_start
loop_end:
mov rax, 60
mov rdi, rcx ; return the highest number as exit code
syscall

118
x86_64/3_jump.asm Normal file
View File

@@ -0,0 +1,118 @@
global _start
; Static data
section .data
promptStr: db 'Write something! (max 32 bytes)',0xA
promptStrSize: equ $ - promptStr
unusedStr: db "This string won't be printed",0xA
unusedStrSize: equ $ - unusedStr
lessStr: db 'The input string has less than 16 bytes',0xA
lessStrSize: equ $ - lessStr
moreStr: db 'The input string has 16 bytes or more',0xA
moreStrSize: equ $ - moreStr
; Dynamic data
section .bss
inputBuffer: resb 32
inputBufferSize: equ 32
; Code
section .text
_start:
mov rax, 1
mov rdi, 1 ; stdout
mov rsi, promptStr
mov rdx, promptStrSize
syscall
jmp request_input
; Skipped over
mov rax, 1
mov rdi, 1
mov rsi, unusedStr
mov rdx, unusedStrSize
syscall
request_input:
xor rax, rax ; 0 = read
xor rdi, rdi ; 0 = stdin
mov rsi, inputBuffer
mov rdx, inputBufferSize
syscall
mov rbx, rax ; store read bytes
; *** FLAGS *** (16 bit era)
; Bit Abbreviation Description
; 0 CF Carry Flag (1 = carry, 0 = no carry)
; 2 PF Parity Flag (1 = even, 0 = uneven)
; 4 AF Adjust Flag (1 = auxiliary carry, 0 no ...)
; 6 ZF Zero Flag (1 = zero, 0 = non zero)
; 7 SF Sign Flag (1 = negative, 0 = positive)
; 8 TF Trap Flag
; 9 IF Interrupt Flag (1 = enabled, 0 = disabled)
; 10 DF Direction Flag (1 = down, 0 = up)
; 11 OF Overflow Flag (1 = overflow, 0 = no overflow)
; 12 --v
; 13 IOPL IO Privilege Level
; 14 NT Nested Task
;
; *** EFLAGS *** (32 bit era)
; 16 RF Resume Flag
; 17 VM Virtual 8086 mode
; 18 AC Alignment check
; 19 VIF Virtual Interrupt Flag
; 20 VIP Virtual Interrupt Pending
; 21 ID CPUID enabled flag
;
; *** RFLAGS *** (64 bit era)
; - - None yet
; Subtraction without storing the result
cmp rbx, 16
; Jump based upon the flags set above
jl less_than_16
; >= 16 bytes
mov rax, 1
mov rdi, 1
mov rsi, moreStr
mov rdx, moreStrSize
syscall
jmp size_printed
less_than_16:
mov rax, 1
mov rdi, 1
mov rsi, lessStr
mov rdx, lessStrSize
syscall
size_printed:
dec rbx ; subtract the newline from the count
xor rcx, rcx ; loop variable
loop_begin:
cmp rcx, rbx ; while loop var < (string size - 1)
jge loop_end
inc byte [inputBuffer + rcx]
inc rcx
jmp loop_begin
loop_end:
inc rbx ; restore original bytes read count
mov rax, 1
mov rdi, 1
mov rsi, inputBuffer
mov rdx, rbx
syscall
mov rax, 60
xor rdi, rdi
syscall ; see ya