ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int 0x80 is for 32bit systems, but you appear to be using 64bit registers so I'll assume you're running on 64bit. Use 'syscall' instead. Also, I'm not convinced you're using the correct registers.
Here's one I made when I was learning this stuff:
Code:
# Hello World in x86_64 AT&T Syntax Assembly, for Linux.
########################################################################
.section .data
msg_text:
.ascii "Hello World, AT&T Assembly style! \n"
.ascii "------------------------------------------------------------------------\n"
.ascii "\n"
.ascii "Exciting... isn't it?\n"
.ascii "\n"
.ascii "------------------------------------------------------------------------\n"
.equ msg_length, . - msg_text
.ascii "\0" # The NUL terminator is not strictly needed as we calculate the
# exact length above, but it makes the string all tidy and 'C' like.
########################################################################
.section .text
.global _start
_start:
mov $1, %rdi # FD for write (stdout)
mov $msg_text, %rsi # pointer to msg text.
mov $msg_length, %rdx # length of write
write:
mov $1, %rax # syscall number for sys_write
syscall # write(1, msg_text, msg_length)
cmp $-4, %rax # Did we get -EINTR? (%rax will contain NEG of errno on error)
je write # if so, try again
cmp $0, %rax # Check for other errors,
jl fail # and fail if we find one.
cmp %rdx, %rax # Did we write it all?
je success # yes, jump to success.
add %rax, %rsi # Adjust the buffer pointer,
sub %rax, %rdx # and length, to account for characters written
jmp write # Go back and try to continue the write
success:
mov $0, %rdi # return value for sys_exit
jmp exit
fail:
mov $1, %rdi # return value for sys_exit
exit:
mov $60, %rax # syscall number for sys_exit
syscall # exit()
########################################################################
I realize I'm not adding to the actual answer here, however having programmed in other assembly languages long, long ago, what I can say is that the code example given by GazL is a very good example of assembly language programming. Please consider the structure and manner of how that example is written for future assembly language programming. Adding comments, and using indents really help you in the future if you happen to revisit former code.
Sorry to ask stupid questions. I am a beginner student. please tell me how to display a three-digit number. sorry for arrogance.
Nobody said your question was stupid, nobody called anyone arrogant.
I'd answer directly if I could, my ASM experience was on 68000 and is 30 years old, plus I've never written to stdout or anything similar, it was pure embedded, with no display.
Meanwhile the example program provided by GazL cites that it outputs Hello World. Perhaps try and see if the assembly language it is written in is suitable for the assembler you're using, and if so, then try modifications to that existing example to output a three digit number, like "100". I'm really not noticing great differences in the opcodes and register names.
I'll stay out of this thread after this point, mainly because as cited, I don't know the answer here at all. Best of luck though!
Anyway, for my own entertainment and education -- Don't be fooled, I'm only a beginner myself when it comes to x86. Motorola and S/390 is where I'm stronger, and I haven't done much of that for many years -- I modified my original as follows:
Code:
# Show a 3digit number:
########################################################################
.section .rodata
value:
.word 457 # Value to show.
########################################################################
.section .data
msg_text:
.string "Value is: \n"
.equ msg_len, . - msg_text - 1
.equ msg_off, msg_len - 2
########################################################################
.section .text
.global _start
_start:
movw value, %ax # Load the Value we want to display into AX.
cmp $999,%rax # Is it too big to display?
jg fail # yes, fail!
# Extract digits from value, one by one ########################
mov $10, %rcx # Set divisor to 10 ready for DIV Instruction.
mov $msg_off, %rdi # offset into msg_text buffer.
extract:
mov $0, %rdx # Prepare for div op.
div %rcx # Use DIV to find remainder.
add $'0', %rdx # convert remainder to ascii character.
movb %dl, msg_text(%rdi) # overwrite character at msg_text + offset.
dec %rdi # Decrease offset (yes, we insert backwards).
cmp $0, %rax # Any more digits to process?
jg extract # yes, loop.
# Write msg_text to stdout: ###################################
mov $1, %rdi # FD for write (stdout)
mov $msg_text, %rsi # pointer to msg text.
mov $msg_len, %rdx # length of write
write:
mov $1, %rax # syscall number for sys_write
syscall # write(1, msg_text, msg_length)
cmp $-4, %rax # Did we get -EINTR? (%rax will contain NEG of errno on error)
je write # if so, try again
cmp $0, %rax # Check for other errors,
jl fail # and fail if we find one.
cmp %rdx, %rax # Did we write it all?
je success # yes, jump to success.
add %rax, %rsi # Adjust the buffer pointer,
sub %rax, %rdx # and length, to account for characters written
jmp write # Go back and try to continue the write
# Exit ########################################################
fail:
mov $1, %rdi # return value for sys_exit
jmp exit
success:
mov $0, %rdi # return value for sys_exit
exit:
mov $60, %rax # syscall number for sys_exit
syscall # exit()
########################################################################
The digits to ascii should really be a subroutine, but I'll need to refresh myself on calling conventions and how to save/restore registers before I attack that one.
Last edited by GazL; 08-02-2019 at 06:28 PM.
Reason: typo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.