LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-10-2011, 11:29 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Referencing the low byte of a constant.


Nasm 0.98.39

Hi:
Suppose I have this constant in an assembly language source program:

FREQ equ 1532

And I want to load the AL register with the low byte of FREQ. Made by hand, as 1532 = 0x05fc, it would be

mov al,0xfc (AT&T mov $0xfc,al)

In MASM (ms-dos) this was done

MOV AL,LOW(FREQ)

Does anybody know of a way of doing this in nasm?
 
Old 07-10-2011, 12:36 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by stf92 View Post
Nasm 0.98.39

Hi:
Suppose I have this constant in an assembly language source program:

FREQ equ 1532

And I want to load the AL register with the low byte of FREQ. Made by hand, as 1532 = 0x05fc, it would be

mov al,0xfc (AT&T mov $0xfc,al)

In MASM (ms-dos) this was done

MOV AL,LOW(FREQ)

Does anybody know of a way of doing this in nasm?
How about using bitwise AND with 0xff ?
 
Old 07-10-2011, 03:02 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Hi:

Thanks for the tip. This is what nasm did:
Code:
  16 00000000 45                      juan            db      0x45
    17                                  
    18                                  section         .text
    19                                          global _start
    20                                  freq equ 10 % 7
    21                                  len  equ 1532
    22                                  len_low equ len & 0xff
    23                                  
    24                                  _start:
    25 00000000 B003                            mov al,freq
    26 00000002 8025[00000000]03                and byte[juan],freq
    27 00000009 B0FC                            mov al,len & 0xff
    28 0000000B B005                            mov al,(len & 0xff00) / 256
    29 0000000D 90                              nop
    30
By the way, how primitive this assembler is! And yet so popular! Look how it does not list the values of freq, len, len_low when declared. And it does not produce a listing file unless there has been no errors in the assembly.
 
Old 07-10-2011, 05:01 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by stf92 View Post
Hi:

Thanks for the tip. This is what nasm did:
Code:
  16 00000000 45                      juan            db      0x45
    17                                  
    18                                  section         .text
    19                                          global _start
    20                                  freq equ 10 % 7
    21                                  len  equ 1532
    22                                  len_low equ len & 0xff
    23                                  
    24                                  _start:
    25 00000000 B003                            mov al,freq
    26 00000002 8025[00000000]03                and byte[juan],freq
    27 00000009 B0FC                            mov al,len & 0xff
    28 0000000B B005                            mov al,(len & 0xff00) / 256
    29 0000000D 90                              nop
    30
By the way, how primitive this assembler is! And yet so popular! Look how it does not list the values of freq, len, len_low when declared. And it does not produce a listing file unless there has been no errors in the assembly.
How about RTFMing ?

http://www.nasm.us/xdoc/2.09.09/html...#section-2.1.3 :

Quote:
2.1.3 The -l Option: Generating a Listing File

If you supply the -l option to NASM, followed (with the usual optional space) by a file name, NASM will generate a source-listing file for you, in which addresses and generated code are listed on the left, and the actual source code, with expansions of multi-line macros (except those which specifically request no expansion in source listings: see section 4.3.11) on the right. For example:

nasm -f elf myfile.asm -l myfile.lst

If a list file is selected, you may turn off listing for a section of your source with[list -], and turn it back on with[list +], (the default, obviously). There is no "user form" (without the brackets). This can be used to list only sections of interest, avoiding excessively long listings.
 
Old 07-10-2011, 05:04 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Code:
(len & 0xff00) / 256
should better be replaced (for clarity) with right non-cyclic shift by 8 bits of 'len'.
 
Old 07-10-2011, 05:16 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I'll try RTFMing, although I picked nasm up because it is the most nearly compatible with MASM I found, and I have a long program written in the MASM assembler that I want to adapt for linux.

About nasm, it sounds incredible, I know, but line 26 of the listing, is not correctly assembled. It says 8025 and it should say 8026. And this translates into the binary file. So either I discard nasm or get a newer version.

But of course I used the -l option. But no listing outpu if there are errors. I wanted to use a shift but I did not know the syntax. Thanks for the post.
 
Old 07-10-2011, 06:47 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by stf92 View Post
I'll try RTFMing, although I picked nasm up because it is the most nearly compatible with MASM I found, and I have a long program written in the MASM assembler that I want to adapt for linux.

About nasm, it sounds incredible, I know, but line 26 of the listing, is not correctly assembled. It says 8025 and it should say 8026. And this translates into the binary file. So either I discard nasm or get a newer version.

But of course I used the -l option. But no listing outpu if there are errors. I wanted to use a shift but I did not know the syntax. Thanks for the post.

http://www.nasm.us/xdoc/2.09.09/html...#section-3.5.4 :


Quote:
3.5.4 << and >>: Bit Shift Operators

<< gives a bit-shift to the left, just as it does in C. So 5<<3 evaluates to 5 times 8, or 40. >> gives a bit-shift to the right; in NASM, such a shift is always unsigned, so that the bits shifted in from the left-hand end are filled with zero rather than a sign-extension of the previous highest bit.
 
1 members found this post helpful.
Old 07-11-2011, 02:37 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks for the help. And for the doc references. Do you know a forum dedicated to assembler stuff?
 
Old 07-11-2011, 06:45 AM   #9
mf93
Member
 
Registered: Jun 2009
Distribution: Debian Squeeze, centOS
Posts: 229

Rep: Reputation: 36
IMO, fasm is a more advanced assembler with nearly identical nasm syntax that is written in assembler, as opposed to nasm being written in c. Also it uses multiple passes to generate a more efficient compilation
 
1 members found this post helpful.
Old 07-11-2011, 07:56 AM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
That sounds attractive. I'll try to get it. Thanks.
 
Old 07-12-2011, 09:35 AM   #11
mf93
Member
 
Registered: Jun 2009
Distribution: Debian Squeeze, centOS
Posts: 229

Rep: Reputation: 36
Here is the site: http://flatassembler.net/ it includes download links and documentation.
 
Old 07-12-2011, 09:40 AM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks. I already downloaded it. It has a drawback: it doesn't output listing files.

Last edited by stf92; 07-12-2011 at 09:41 AM.
 
  


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
[SOLVED] memcpy fails to copy data, but byte by byte assignment work venu_s Programming 7 07-08-2011 03:29 AM
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM

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

All times are GMT -5. The time now is 05:09 PM.

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