Вопрос

There's something wrong with "messages" block and it's output (print_less, print_more, print_equal, print_final). I really don't know how to store strings, so that's why I'm getting all of them in my output instead of just two of them (print_less or print_more or print_equal + print_final).

Here's the code:

format ELF64 executable


;=============================================
;================== DATA =====================
;=============================================
segment readable writeable
;=============================================


;=============================================
;                 variables
;=============================================

a dq 26h
b dq 100h 
c dq 4h
d dq 3h


;=============================================
;                 messages
;=============================================

msg_less db 'a < b',0xA,0
msg_more db 'a > b',0xA,0
msg_equal db 'a = b',0xA,0
msg_final db 'Completed successfully',0xA,0


;=============================================
;               message sizes
;=============================================

msg_more_size = $-msg_more
msg_less_size = $-msg_less
msg_equal_size = $-msg_equal
msg_final_size = $-msg_final


;=============================================
;================== CODE =====================
;=============================================
segment readable executable
;=============================================

entry $

_begin:
    mov rcx,[a]
    cmp rcx,[b]

    jl  print_less
    jg  print_more
    je  print_equal

_final:
    mov edx,msg_final_size
    lea rsi,[msg_final]

    mov edi,1   ; STDOUT
    mov eax,1   ; sys_write
    syscall

    jmp syscall_exit


;=============================================
;             print message segment
;=============================================

print_less:
    mov edx,msg_less_size
    lea rsi,[msg_less]
    jmp syscall_msg

print_more:
    mov edx,msg_more_size
    lea rsi,[msg_more]
    jmp syscall_msg

print_equal:
    mov edx,msg_equal_size
    lea rsi,[msg_equal]
    jmp syscall_msg


;=============================================
;               syscall segment
;=============================================

syscall_msg:
    mov edi,1   ; STDOUT
    mov eax,1   ; sys_write
    syscall

    jmp _final

syscall_exit:
    xor edi,edi ; exit code 0
    mov eax,60  ; sys_exit
    syscall

output:

[look@me ~]$ fasm ~/workspace/ASM/ASM1/asm.asm && ~/workspace/ASM/ASM1/asm
flat assembler  version 1.70.03  (16384 kilobytes memory)
3 passes, 364 bytes.
a < b
a > b
a = b
Completed successfully
Completed successfully
Это было полезно?

Решение

Your length calculations are incorrect. For instance the value of msg_more_size would be:

msg_more_size = msg_equal-msg_more

$ is actually denotes where your current code location is. So $-msg_more means almost the whole string.

Другие советы

Probably because your strings are not Null terminated. Add 0 at the end of your strings and try out again .

msg_less db 'a < b',0xA, 0 ; <-- Null added

That because print command continues to print characters, until it encounters null.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top