NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: nobody on September 23, 2009, 02:05:30 PM

Title: conversion of at&t to nasm
Post by: nobody on September 23, 2009, 02:05:30 PM
I've finally decided to take the plunge and learn assembly language, something I've always been interested in doing. I have a few books on Computer Architecture because I realize I need an understanding of that as well. I'm trying to convert the second program example of 'Programming from the ground up' and I've narrowed the mistakes down to just two lines and I cant get any further. The docs for nasm itself are'nt much help at the moment. I use amd64 with slackware 13. Can anyone help with code and explanations please, thank you. David.

Here is the original code

; eax - current data item
; ebx - largest data item found
; edi -  holds the index of the data item being examined

.section .data

data_items:

.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0

.section text

.globl _start

_start:

movl $0, %edi

movl data_items(,%edi,4), %eax

movl %eax, %ebx

start_loop:

cmpl $0, %eax

je loop_exit

incl %edi

movl data_items(,%edi,4), %eax

cmpl %ebx, %eax

jle start_loop

movl %eax, %ebx

jmp start_loop

loop_exit:

movl $1, %eax

int $0x80

Ok here is my code converted to nasm as best I can. My trouble is

with indexed addressing, I've tried numerous variations but nothing

works.

;maximum.asm

;this program finds the maximum number of a set of data items
;
;VARIABLES: the registers have the following uses:
;
;   rax: current data item
;   rbx: largest data item found
;   rdi: holds the index of the data item being examined
;
;the following memory locations are used
;
;   data_items: contains the item data. 0 is used to terminate the data

section .text
global _start

_start:

mov rdi,0
mov rax,data_items,[rdi+4]
mov rbx,rax

start_loop:

cmp rax,0
je loop_exit
inc rdi
mov rax,data_items,[rdi+4]
cmp rax,rbx
jle start_loop
mov rbx,rax
jmp start_loop

loop_exit:

mov rax,1
int 0x80

section .data

data_items:

db   3,64,23,56,78,44,87,35,54,0

Looking forward to all replies and advice, thanks.
Title: Re: conversion of at&t to nasm
Post by: Frank Kotler on September 23, 2009, 03:18:14 PM
Welcome to the world of assembly language! ("Abandon hope, all ye who enter here") :)

I think "data_items" is supposed to be "dd" not "db" (".long"). For 64-bit code, you might want "dq".

The basic syntax you want would be "mov rax, [data_items + rdi * 8]" ( "* 4" if "dd"). 64-bit code uses a weird "rip-relative" addressing mode (nice idea, but "different") so this may not work correctly - or maybe it would.... Also, 64-bit code apparently uses "syscall" instead of "int 80h"... and the sys_call numbers are completely different! :(

Wilhelm Zadrapa has kindly translated the PGU examples to Nasm syntax:

http://home.myfairpoint.net/fbkotler/nasm-pgu-examples.tar.bz2 (http://home.myfairpoint.net/fbkotler/nasm-pgu-examples.tar.bz2)

These are for 32-bit code. You might find it easier to do 32-bit code first, and work up to 64-bit (it is *quite* different!!!). To get 64-bit ld to link 32-bit code, you'll need "-melf-i386" (I think that's correct) otherwise it tries to make 64-bit code and gets confused...

In any case, in Nasm syntax, all of the memory reference needs to be between the "[]"s.

Mmmm, I see SoreFog has taken "data_items" as a sign to use italics, and removed the underscore... Whatever...

Best,

Frank
Title: Re: conversion of at&t to nasm
Post by: Frank Kotler on September 23, 2009, 03:35:58 PM
I should have mentioned, there's a tutorial on 64-bit code:

http://www.vikaskumar.org/wiki/index.php?title=X86-64_Tutorial (http://www.vikaskumar.org/wiki/index.php?title=X86-64_Tutorial)

Also some information here:

http://milw0rm.org/papers/110 (http://milw0rm.org/papers/110)

You may find these useful if you're going to translate the PGU examples to 64-bit. Really very different!

Thanks for that info, guys!

Best,
Frank
Title: Re: conversion of at&t to nasm
Post by: nobody on September 24, 2009, 02:06:31 AM
Ok, this is perfect! Thank you very much. That's a major stumbling block out of the way, should make learning a lot easier now. Thanks for a quick reply too. Dave