assembly - Access violation gas read syscall -
i new assembly language , facing problem in first steps. have experience high level languages feel total beginner in low level.
i using gnu assembler (gas) version 2.23 x86_64 on xubuntu system.
i wrote simple hello world , want extend following features:
- read input using syscall 3
- check if input greater specified
- if shorter or equal print input
- else print error message
if run code works shorter input, if type in more 5 characters, access violation.
i work this (german) tutorial.
here code:
# # author: linluk # filename: name.s # # usage: # $ name.s -o name.o # $ ld name.o -o name # $ ./name # .section .data hello: .ascii "hello, name?\n" err: .ascii "too long!\n" name: .ascii "" .section .text .globl _start _start: #output mov $4, %eax # 4 syscall write mov $1, %ebx # 1 filedescriptor stdout mov $hello, %ecx # output buffer mov $26, %edx # length of buffer int $0x80 # call #input mov $3, %eax # 3 syscall read mov $0, %ebx # 0 filedescriptor stdin mov $(name), %ecx # address of name mov $6, %edx # max length int $0x80 #check input length mov $5, %ebx ####################### cmp %ebx, %eax # <-- here or # jg problem #to long # <-- here # # wrong # # guess !! # #output of $name ####################### mov $1, %ebx mov $name, %ecx mov %eax, %edx mov $4, %eax int $0x80 jmp exit problem: #output of $err mov $4, %eax mov $1, %ebx mov $err, %ecx mov 10, %edx int $0x80 exit: mov $1, %eax # 1 syscall exit mov $0, %ebx # returnvalue int $0x80
i way have done homework in advance, , shown written code.
i super fact commented code; excellent sir. i'm happy (if indeed helpful).
okay, i'll focus on stuff...
mov $5, %ebx ####################### cmp %ebx, %eax # <-- here or # jg problem #to long # <-- here # # wrong # # guess !! # #output of $name ####################### worry not; nothing's wrong. goes through (including me).
wow, start.
okay, got it, 15 minute exercise clear matter you.
first, question: familiar signed opposed unsigned math @ register , bit level ? if not, prepare brain cell activity.
secondly, confuse things further, assembler nomenclature , syntax have effect on result can expect (and wind getting).
i'm not savvy gnu assembler, let me explain how got grasp on this.
the instruction cmp this,that can thought mean like...
- what relationship of
thisrespectthat?
more point, this above, equal, or below that ?
i stick unsigned integer now; because it's simplest.
not sure gnu, other assemblers, when using unsigned thinking in brain, want use instructions ja , jb , je comparisons.
ja "jump if above" jb "jump if below" je "jump if equal"
the jg using could be signed arithmetic; we'll worry later.
to confuse things more, assembler syntax can reverse order of operands, , need alter mental state , think this question instead of original one...
- what relationship of
thatrespectthis?
okay, now, setting breakpoints, here's how mind this..
mov $5, %ebx # original way cmp %ebx, %eax # same compare ja problem # unsigned maybe jump mov $5, %ebx # same steps cmp %ebx, %eax # same steps jb problem # opposite jump sense mov $5, %ebx # now, similar steps cmp %eax, %ebx, # comparing them in opposite manner ja problem # watch see if jump hits mov $5, %ebx # arrange them again cmp %eax, %ebx, # backwards compare, but,,,, jb problem # opposite opinion of flags these 4 examples assume 2 numbers (in ebx , eax) different. if turn out same, have extended discussion on matter day.
this is, again, unsigned integer arithmetic. some, elementary stuff; others, worry not, struggling around confusion is, honestly, norm. know did it.
anyway, set breakpoints , run each of 3 line snippets, , idea in head, "...what relationship of this respect that ?..."
as step through each 3 line snippet, you'll idea.
if screw up, make sense second (or third or fourth or fifth) time.
not sure if helping or not, thought type others new regs , bits , flags , stuff.
Comments
Post a Comment