Author Topic: beginner questions  (Read 50459 times)

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #90 on: March 31, 2016, 07:29:08 AM »
Hi Bryant,
Yes, the hd is ntfs created by windows.
I put the NASM files on this drive.
I will try this again in case I was in the wrong place .

jack@jack-myheadache ~/nasmfld $ sudo umount /media/jack/nasm
[sudo] password for jack:
umount: /media/jack/nasm: not found
jack@jack-myheadache ~/nasmfld $

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #91 on: March 31, 2016, 07:34:09 AM »
Linux is case-sensitive. nasm and NASM are two different things. Try again:

sudo umount /media/jack/NASM
sudo mount -t ntfs -o rw,auto,user,fmask=0022,dmask=0000,exec /dev/sda5 /media/jack/NASM


About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #92 on: March 31, 2016, 11:55:20 AM »
Code: [Select]
#1
HI Bryant,
The HD is always open as it is a windows drive.

jack@jack-myheadache /media/jack/NASM $ sudo umount /media/jack/NASM
[sudo] password for jack:
umount: /media/jack/NASM: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
jack@jack-myheadache /media/jack/NASM $

I don't want to be a pest about this so I think another approach
might be in order. How about me just keeping Nasm up to date on
the HD? I can work on the SSD with Linux and Nasm. Should either
one fail I can restore Linux from the DVD and Nasm from the HD.
Not what I want but doable.

#2
Should I try to update Nasm to the latest version 2.12.01.
The only way I could find out what version of Nasm I am running
is by looking at the zip file I downloaded. It is v2.11.08

#3
                  4-2-2016
I am trying to get the year, month and day.
Not sure if any of this is right.

   extern time.h

   struc tm
     int tm.sec        ; seconds   ( 'INT" DO I NEED IT??)
     int tm.min        ; minutes
     int tm.hour       ; hours
     int tm.mday       ; day of the month
     int tm.mon        ; month
     int tm.year       ; year
     int tm.wday       ; day of the week
     int tm.yday       ; day in the year
     int tm.isdst      ; daylight saving time
   endstruc

   invoke GetLocalTime, addr tm  ; 449
    GetLocalTime is a Windows instruction
    Don't know what to replace it with?

   movzx eax,W[tm.year]            ; 450
   movzx ecx,W[tm.mon]            ; 451
   movzx ebx,W[tm.mday]          ; 452

jack@jack-myheadache ~/nasmfld $ nasm -f elf64 demo64.asm -o demo64.o
demo64.asm:449: error: parser: instruction expected
demo64.asm:450: error: comma, colon or end of line expected
demo64.asm:451: error: comma, colon or end of line expected
demo64.asm:452: error: comma, colon or end of line expected
jack@jack-myheadache ~/nasmfld $

 
« Last Edit: April 02, 2016, 08:44:02 PM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #93 on: April 03, 2016, 06:05:24 AM »
#1
HI Bryant,
The HD is always open as it is a windows drive.

jack@jack-myheadache /media/jack/NASM $ sudo umount /media/jack/NASM
[sudo] password for jack:
umount: /media/jack/NASM: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
jack@jack-myheadache /media/jack/NASM $

I don't want to be a pest about this so I think another approach
might be in order. How about me just keeping Nasm up to date on
the HD? I can work on the SSD with Linux and Nasm. Should either
one fail I can restore Linux from the DVD and Nasm from the HD.
Not what I want but doable.

The reason the device is busy is because your currently in the directory. You have to run that command from somewhere other than on the drive.

#2
Should I try to update Nasm to the latest version 2.12.01.
The only way I could find out what version of Nasm I am running
is by looking at the zip file I downloaded. It is v2.11.08

You can tell your version by using the command nasm -v. The version I'm currently using is v2.11.07, so you're pretty up to date... I think I need to run updates. :D

#3
                  4-2-2016
I am trying to get the year, month and day.
Not sure if any of this is right.

   extern time.h

   struc tm
     int tm.sec        ; seconds   ( 'INT" DO I NEED IT??)
     int tm.min        ; minutes
     int tm.hour       ; hours
     int tm.mday       ; day of the month
     int tm.mon        ; month
     int tm.year       ; year
     int tm.wday       ; day of the week
     int tm.yday       ; day in the year
     int tm.isdst      ; daylight saving time
   endstruc

   invoke GetLocalTime, addr tm  ; 449
    GetLocalTime is a Windows instruction
    Don't know what to replace it with?

   movzx eax,W[tm.year]            ; 450
   movzx ecx,W[tm.mon]            ; 451
   movzx ebx,W[tm.mday]          ; 452

jack@jack-myheadache ~/nasmfld $ nasm -f elf64 demo64.asm -o demo64.o
demo64.asm:449: error: parser: instruction expected
demo64.asm:450: error: comma, colon or end of line expected
demo64.asm:451: error: comma, colon or end of line expected
demo64.asm:452: error: comma, colon or end of line expected
jack@jack-myheadache ~/nasmfld $

Nope, none of that is right.  :P It's okay though, we can fix it!  ;D

The extern directive is for specifying symbols (like the names of functions or variables) that are defined outside of our code. In your code, time.h is wrong because that's the name of a header file from the standard C library.

The structure portion is wrong too. The int's you are seeing are not interrupt instructions. In C they have a type called int which represents integers.

Invoke is not an instruction, it's a macro. Microsoft's MASM has many built-in macros for HL development. NASM doesn't have very many built-in macros so you do things the long way or use a macro package like NASMX.

The following code might help as a test for working with time. I think C integers are still 8-byte DWORD types so I defined them that way here. The 32-bit version of this code worked fine here, let's hope this 64-bit translation still works.  ;D

Code: (ntime.asm) [Select]
;;; ntime.asm - displays the current time information.

    BITS 64

    GLOBAL main

    %define STACK_ALIGN     sub rsp, 8 ; stack adjustment for calling functions.
    %define STACK_RESTORE   add rsp, 8 ; restore previous stack alignment.

;;; Procedures from the standard C library.
    EXTERN time
    EXTERN localtime
    EXTERN printf

;;; Data-type from the C time.h header.
    STRUC tm
    .tm_sec      resd 1         ; Seconds (0-60)
    .tm_min      resd 1         ; Minutes (0-59)
    .tm_hour     resd 1         ; Hours (0-23)
    .tm_mday     resd 1         ; Day of the Month (1-31)
    .tm_mon      resd 1         ; Month (0-11)
    .tm_year     resd 1         ; Year - 1900
    .tm_wday     resd 1         ; Day of the week (0-6, Sunday = 0)
    .tm_yday     resd 1         ; Day in the year (0-365, 1 Jan = 0)
    .tm_isdst    resd 1         ; Daylight saving time
    ENDSTRUC

SECTION .data                   ; Intialized data section.

now:    dq 0                    ; Storage for time since Epoch
plt:    dq 0                    ; Storage for pointer returned by localtime

SECTION .rodata                 ; Read-only data section.

strTime:    db "The current time is %d:%d:%d", 10, 0

SECTION .text                   ; Executable data section.

    ;; main : number (string . string) -> number .
    ;; The application entrypoint (main) consumes an
    ;; argument count (argc) and a list of arguments
    ;; from the command line (argv) and produces an
    ;; error code for the operating system.
main:
    STACK_ALIGN

    ;; Quote from (The Linux Programmer's Manual):
    ;;  time() returns the time as the number of seconds since the
    ;;  Epoc, 1970-01-01 00:00:00 +0000 (UTC).
    ;;
    ;;  If `t' is non-NULL, the return value is also stored in the
    ;;  memory pointed to by `t'.
   
    mov rdi, now                ; now = t
    call time                   ; get time

    ;; Quote from (The Linux Programmer's Manual):
    ;;  The localtime() function converts the calendar time `timep'
    ;;  to broken-down time representation, expressed relative to the
    ;;  user's specified timezone.
   
    mov rdi, now                ; timep = now
    call localtime              ; convert to localtime
    mov [plt], rax              ; save pointer to tm structure.
   
    ;; ... do some other stuff ...

    mov rdi, [plt]              ; get pointer to local tm structure.
   
    movzx rcx, dword [rdi + tm.tm_sec]   ; get second
    movzx rdx, dword [rdi + tm.tm_min]   ; get minute
    movzx rsi, dword [rdi + tm.tm_hour]  ; get hour
    mov rdi, strTime                     ; show time
    call printf

    mov rax, 0                  ; return EXIT_SUCCESS to Linux
    STACK_RESTORE
    ret

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #94 on: April 03, 2016, 10:01:52 AM »
Code: [Select]
#1
          4-3-2016
Hi Bryant,
I am trying to install nasm 2.12.01
Only place I can see to install NASM is in the zip
expansion "install".
It is only instructions to install which don't
work for me.
I renamed the previous nasmfld to nasmfld2.
Made another blank nasmfld.

jack@jack-myheadache ~/nasmfld $ sh autogen.sh
sh: 0: Can't open autogen.sh
jack@jack-myheadache ~/nasmfld $
 
#2
   Does "resd 1" mean dword? What does the "1" do?
      .tm_sec   resd 1 

#3
Everything I see talks about "printf" going to the screen.
I need to go to a hardware printer. Where can I read how to
do this?

#4
Can't find out how to draw lines from A to B.
This has nothing to do with macros.
Still looking....
« Last Edit: April 05, 2016, 01:20:17 PM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #95 on: April 06, 2016, 02:43:21 AM »
#1
          4-3-2016
Hi Bryant,
I am trying to install nasm 2.12.01
Only place I can see to install NASM is in the zip
expansion "install".
It is only instructions to install which don't
work for me.
I renamed the previous nasmfld to nasmfld2.
Made another blank nasmfld.

jack@jack-myheadache ~/nasmfld $ sh autogen.sh
sh: 0: Can't open autogen.sh
jack@jack-myheadache ~/nasmfld $

Code: [Select]
$ wget http://www.nasm.us/pub/nasm/releasebuilds/2.12.02rc1/nasm-2.12.02rc1.tar.gz
--2016-04-05 21:59:50--  http://www.nasm.us/pub/nasm/releasebuilds/2.12.02rc1/nasm-2.12.02rc1.tar.gz
Resolving www.nasm.us (www.nasm.us)... 198.137.202.10, 2001:1868:205::10
Connecting to www.nasm.us (www.nasm.us)|198.137.202.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1213813 (1.2M) [application/gzip]
Saving to: ‘nasm-2.12.02rc1.tar.gz’

nasm-2.12.02rc1.tar 100%[=====================>]   1.16M   159KB/s   in 7.4s   

2016-04-05 21:59:58 (160 KB/s) - ‘nasm-2.12.02rc1.tar.gz’ saved [1213813/1213813]

$ tar xvf nasm-2.12.02rc1.tar.gz
nasm-2.12.02rc1/
nasm-2.12.02rc1/preproc.h
nasm-2.12.02rc1/AUTHORS
... a bunch of other files...
$ cd nasm-2.12.02rc1/
nasm-2.12.02rc1$ ./configure && make && sudo make install

The last command will configure, build, then prompt you for your password before installing to the system.


#2
   Does "resd 1" mean dword? What does the "1" do?
      .tm_sec   resd 1 

Documentation for RESD


#3
Everything I see talks about "printf" going to the screen.
I need to go to a hardware printer. Where can I read how to
do this?

I haven't tried this code, but in theory you should do this:
Code: [Select]
;; Printing text to hardware.

BITS 64

GLOBAL main
EXTERN popen
EXTERN pclose
EXTERN fprintf
EXTERN fflush

%define EXIT_SUCCESS 0
%define EXIT_FAILURE 1

%define STACK_ALIGN sub rsp, 8
%define STACK_RESTORE add rsp, 8

SECTION .rodata

szPrinter: db "lpr", 0
szWriteMode: db "w", 0

szSomeMessage: db "Hello, world!\nThis is a PDF!", 0

SECTION .data

fdPrinter: dq 0 ; our file descriptor.

SECTION .text

main: STACK_ALIGN
mov rsi, szWriteMode
mov rdi szPrinter
call popen
mov [fdPrinter], rax

cmp rax, 0
jnz .okay

mov rax, EXIT_FAILURE
STACK_RESTORE
ret

.okay:
mov rsi, szSomeMessage
mov rdi, [fdPrinter]
call fprintf

mov rdi, [fdPrinter]
call pclose

mov rax, EXIT_SUCCESS
STACK_RESTORE
ret


#4
Can't find out how to draw lines from A to B.
This has nothing to do with macros.
Still looking....

As I said earlier in this thread, you're not going to be able to draw graphics in the terminal, that's not what it's for. You'll need to use one of the libraries I suggested earlier to draw on the console, or bite the bullet and learn to do GUI applications.

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #96 on: April 06, 2016, 09:57:49 AM »
...
« Last Edit: April 06, 2016, 08:09:07 PM by jackjps »

Offline spoomyfly

  • Jr. Member
  • *
  • Posts: 3
Re: beginner questions
« Reply #97 on: January 03, 2017, 10:30:30 AM »
Excuse me, i need help with my code(nasm). May i post it here?(im a newbie)

Offline Eght

  • Jr. Member
  • *
  • Posts: 3
Re: beginner questions
« Reply #98 on: January 03, 2017, 12:16:08 PM »
Hello guys.
My task is to change code from MASM to NASM. I changed the code a little bit but it is still not enough. The most important part is the print section. I've seen the documentation about nasm and masn differences, but everything is like a black magic for me. Can you guys give me some advices?

This is sieve of eratosthenes

 
Code: [Select]
; size of array to hold the prime candidates
; (arraySize - 1) is the max prime candidate.

SECTION .data


    arraySize EQU 10000
    array1: dd arraySize dup(0) ; initialize array

    .code

SECTION .text

extern printf
extern scanf

global main

main:

    cls

    xor ecx, ecx ; zeroing, is slightly faster than MOV reg, 0

   fillArrayLoop:
  mov eax, ecx ; Move counter value to eax
  add eax, 2 ; The sieve starts at 2, therefore add 2 to the loop counter
  mov [array1+4*ecx], eax ; Insert value into array
  inc ecx ; Increment counter
  cmp ecx, arraySize ;
  jb fillArrayLoop ; jump below, Jump to loop start is we are not finished



    xor ecx, ecx ; Zero out counter
    loop1: ; This is out outer loop
mov ebx, ecx ; Prepare ebx to be used af counter in innerloop (loop2)
inc ebx ; Inner loop starts at ecx + 1
  cmp [array1+4*ecx], -1 ; Value discarded? So is value -1?
  jne loop2 ; if not jump to inner loop(loop2)
  resume1: ; Resume point
inc ecx ; Increment out primary counter
cmp ecx, arraySize ; Are we done?
jb loop1 ; If not jump to loop start.
jmp theEnd ; All done!

loop2: ; Inner loop
cmp [array1+4*ebx], -1 ; Value discarded?
jne loop3 ; if nor jump to loop3
resume2: ; Resumt point
inc ebx ; Increment inner loop counter
cmp ebx, arraySize ; Are we done?
jb loop2 ; if not go to loop start.
jmp resume1 ; Loop2 dont, return to loop1.
loop3: ; Here we will test if it is a prime...
xor edx, edx ; Zero out edx
xor eax, eax ; Zero out eax
mov eax, [array1+4*ebx] ; Place the number we want to divite in eax
div [array1+4*ecx] ; Divide the numbe in eax with the value from outer loop
cmp edx, 0 ; Check edx for remainder
je nukeIt ; If we have no remainder it the number in eax is not a prime
; therefore we nuke it. (set it to -1)
resume3: ; Resume point
jmp resume2 ; Done, jump to resume point in loop2
    nukeIt:
    mov [array1+4*ebx], -1 ; Not a prime, set it to -1
    jmp resume3 ; Jump to resume point

    printArrayElement: ; Just printing all the primes.
    push ecx ; Preserve ecx
    print str$([array1+4*ecx]), 10, 13 ; Using the masm32 print macro
    inc ebx ; Incrementing a counter used to store the number of primes.
    pop ecx ; Callback the preserved ecx
    jmp resumePrintArray ; Jump to resumepoint in print array.


   theEnd: ; exit point


    xor ecx, ecx ; Zero out.
    xor ebx, ebx ; Zero out.

    printArray:
    cmp [array1+4*ecx], -1 ; Check if we have a prime.
    jne printArrayElement ; If we have jump to printArrayElement.
    resumePrintArray: ; Resume point
    inc ecx ; Incrementing
    cmp ecx, arraySize ; Are we done?
    jb printArray ; If not jump to printArray...

print "Number of primes below "
print str$(arraySize)
print ": "
print str$(ebx), 10, 13

    ret


and console output :
Code: [Select]
nasm -f elf primes.asm && gcc -m32 -o primes primes.o
primes.asm:9: error: comma expected after operand 1
primes.asm:71: error: parser: instruction expected
primes.asm:91: error: symbol `print' redefined
primes.asm:91: error: parser: instruction expected
primes.asm:92: error: symbol `print' redefined
primes.asm:92: error: parser: instruction expected
primes.asm:93: error: symbol `print' redefined
primes.asm:93: error: parser: instruction expected
primes.asm:94: error: symbol `print' redefined
primes.asm:94: error: parser: instruction expected
« Last Edit: January 03, 2017, 12:30:41 PM by Eght »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: beginner questions
« Reply #99 on: January 03, 2017, 07:52:49 PM »
Hello Spoomyfly,

Welcome to the forum! Yes, you may post your code here. (that was easy!)

You probably want to start a new topic. If possible, use a title that will help someone with similar questions find it. If possible, use "code tags" - just the word "code" in square brackets at the start of your code, and "/code" at the end. Note that this is square brackets "[]" - like a Nasm memory reference - not angle brackets "<>" like html. To help us help you, tell us what you "expect" your code to do, and what it does instead. Looking forward to hearing from you.

Hi Eght,

Welcome to the forum. You already know how to use "code tags". Thanks.

As indicated in your comments, "print" needs to be a macro. Since you're linking to the C library "printf", you probably want to use that, right? "print str$"  is also a macro - also using "printf", probably. We could do this all in one macro and "if the first parameter is str$, print a number" but it is probably easier to use a separate macro - "printint", perhaps. Looks like "cls" also wants to be a macro - I'm surprised you're not getting a warning on that - old version of Nasm? Nasm doesn't use "dup".  Could use "times"...
Code: [Select]
section .data
array times arraySize dd 0
but might be better to do:
Code: [Select]
section .bss
array resd arraySize
Programmer's choice. ("section .bss" is nominally uninitialized, but is, in fact, initialized to zero in any reasonable OS). Speaking of OS, Nasm's "-f elf" is for Linux. Your Masm code almost certainly is not.  Is that what you intend? (no problem - we can do it either way... maybe both...)

You might want to start a new topic rather than append to this rather old one...

Best,
Frank