LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2019, 02:21 PM   #1
slcklnx
Member
 
Registered: Jan 2019
Posts: 144

Rep: Reputation: Disabled
gas assembler


who knows assembler please tell me why not a symbol on the screen?



Code:
.section .bss
var: space 1
.section .text
.global main
main:
mov $'1', %rdx
mov %rdx, (var)
mov $4, %rax
mov $1, %rbx
mov var, %rcx
int $0x80
mov $1, %rax
 
int $0x80
 
Old 08-01-2019, 05:45 PM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
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()

########################################################################
Hope that helps.
 
2 members found this post helpful.
Old 08-02-2019, 08:07 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 08-02-2019, 11:55 AM   #4
slcklnx
Member
 
Registered: Jan 2019
Posts: 144

Original Poster
Rep: Reputation: Disabled
Code:
.section .bss
var: space 3
.section .text
.global main
main:
mov $111, %rdx
mov %rdx, (var)
mov $4, %rax
mov $1, %rbx
mov $var, %rcx
mov $3, %rdx
int $0x80
mov $1, %rax
 
int $0x80


Sorry to ask stupid questions. I am a beginner student. please tell me how to display a three-digit number. sorry for arrogance.

Last edited by slcklnx; 08-02-2019 at 11:56 AM.
 
Old 08-02-2019, 12:18 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by slcklnx View Post
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!
 
2 members found this post helpful.
Old 08-02-2019, 06:28 PM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
I'm getting a deja vu feeling

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
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GNU Assembler (GAS/"as"): How to emit 8 bit relative branches? tedn Linux - Software 4 03-15-2012 10:16 PM
GAS : Assembler Code for writing to memory :Segfault Saptarshi4031 Linux - Software 4 07-20-2010 03:33 AM
gas - GNU Assembler - Source Platform yitzle Linux - Software 2 02-24-2007 08:32 PM
2.00 for a gallon of gas jaz General 251 04-16-2006 12:39 AM
KDE gas plant antipop Linux - Software 3 06-30-2004 08:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration