Author Topic: beginner questions  (Read 58331 times)

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
beginner questions
« on: February 17, 2016, 11:35:57 AM »
1st The captcha needs some work. Keeps on popping up after I have done it.
My 2nd post so please understand. Old windows programmer and absolutely green
with NASM.
Running Linux Mint 17.3 Rosa'

I started with NASM in the Linux Mint forum. That was a mistake.
Did not know about this forum.
From the Linux Mint forum I got the Linux System Call Table 64. Is this any good???
I got their test  Hello World program to work.
Moving on I have some questions:
1. how do I clear the screen
2. how do I put a colored rectangle on the screen
3.how do I put characters entered on the screen
4. how do I read the characters on the screen
5. Do I need any C functions. I don't do "C".
6. Does Printf write to the screen or the printer or both
7. where can I get a System call table 64-bit

I am trying to dump Windows.
Will have to rewrite hundreds of programs :'(
Would appreciate any help on getting started here.
Thanks for this site.
 



« Last Edit: February 17, 2016, 02:05:37 PM by jackjps »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #1 on: February 17, 2016, 11:32:43 PM »
Hi jackjps,

Welcome to the Forum, and to Nasm, and to Linux. I hope that we'll be able to help you some - no guarantees!

To start with your last question, the system call list you got from the Mint forum "should" be okay. I thought we had one here:
http://forum.nasm.us/index.php?topic=1045.0
but that's just 32-bit. (has error numbers too)
There is definitely 32- and 64-bit system call numbers included in the NASMX macro package:
http://www.nasmx.sourceforge.net

Working backwards... "printf" writes to STDOUT - usually the screen, but it can be redirected to another file, including the printer.

No, you do not need C. That may sound strange, considering that both Nasm and Linux are written in C, but C doesn't have access to any instructions that are not available in assembly language. C has to access the kernel the same way we do - int 80h for 32-bit and the "syscall" instruction for 64-bit. It is quite convenient to just call code that someone else has written (and tested and maybe even optimized) but you don't "have" to (and don't let anyone tell you you do!)

I'll get back to your earlier questions, but as a quick hint - "VT100" (like ansi.sys in DOS, if you go back that far) and "in Unix, everything is a file".

Later,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #2 on: February 18, 2016, 05:39:28 AM »
This is not what I had  in mind! I came across it looking for something else. It is from Jeff Duntemann - looks like it's half-converted from DOS to Linux. The comments are still mostly DOS. I don't know where I got it. It shows some of the vt100 escape sequences. The remaining DOS parts (int 10h) will not work! The 32-bit Linux parts (int 80h) will need work to convert to 64-bit... but should work as 32-bit code. It may serve as a partial answer to how to do clearscreen. I'm going to post it while I've got my finger on it, although it is not working code. Apologies to Jeff for posting stuff he obviously wasn't finished with.
Code: [Select]
;  Source name     : VIDLIB.ASM
;  Compiled name   : VIDLIB.OBJ
;  Code model:     : Real mode segmented model
;  Version         : 1.0
;  Created date    : 9/12/1999
;  Last update     : 9/12/1999
;  Author          : Jeff Duntemann
;  Description     : A simple example of a separately assembled module
;                    containing utility procedures for controlling the
;                    PC display. Assembled using NASM 0.98.  DOS programs
;                    can link to these routines by declaring them EXTERN
;                    and then linking the program .OBJ to VIDLIB.OBJ using
;                    a linker like ALINK.



;----------------------------|
;     BEGIN DATA SEGMENT     |
;----------------------------|
           SEGMENT .data

;Note that the following items are defined externally to this module, and
;  for certain routines in this module to function these data items must
;  be linked in from a properly assembled external module.

           EXTERN  CRLF,LRXY

;----------------------------|
;     BEGIN CODE SEGMENT     |
;----------------------------|

           SEGMENT .text  ; This segment may be accessed externally

; Note that the following items are GLOBAL, and may be accessed by
;   external files that declare them EXTERN.

           GLOBAL GotoXY,ClrScr,ClrWin,ScrlWin,VIDEO6
           GLOBAL Write,Writeln


;---------------------------------------------------------------
;   GOTOXY    --  Positions the hardware cursor to X,Y
;   Last update 9/12/99
;
;   1 entry point:
;
;   GotoXY:
;      Caller must pass:
;      DL: X value     These are both 0-based; i.e., they
;      DH: Y value       assume a screen 24 by 79, not 25 by 80
;      Action:  Moves the hardware cursor to the X,Y position
;               loaded into DL and H.
;---------------------------------------------------------------
GotoXY:
    sub esp, 16
    mov edi, esp
    mov byte [edi], 27 ; "ESC"
    inc edi
    mov byte [edi], '['
    inc edi
    mov al, dh
    call al2dec
    mov byte [edi], ';'
    inc edi
    mov al, dl
    call al2dec
    mov byte [edi], 'H'
    inc edi
    mov byte [edi], 0
    mov edx, esp
    call Write
    add esp, 16
           ret               ; Return to the caller


al2dec:
    xor ecx, ecx
    mov bl, 10
.pushloop:
    mov ah, 0
    div bl
    push eax
    inc ecx
    cmp al, 0
    jnz .pushloop
.poploop:
    pop eax
    mov al, ah
    add al, '0'
    stosb
    loop .poploop
    ret

;---------------------------------------------------------------
;   CLRSCR    --  Clears or scrolls screens or windows
;   Last update 9/12/99
;
;   4 entry points:
;
;   ClrScr:
;      No values expected from caller
;      Action:  Clears the entire screen to blanks with 07H as
;               the display attribute
;
;   ClrWin:
;      Caller must pass:
;      CH: Y coordinate, upper left corner of window
;      CL: X coordinate, upper left corner of window
;      DH: Y coordinate, lower right corner of window
;      DL: X coordinate, lower right corner of window
;      Action:  Clears the window specified by the caller to
;               blanks with 07H as the display attribute
;
;   ScrlWin:
;      Caller must pass:
;      CH: Y coordinate, upper left corner of window
;      CL: X coordinate, upper left corner of window
;      DH: Y coordinate, lower right corner of window
;      DL: X coordinate, lower right corner of window
;      AL: number of lines to scroll window by (0 clears it)
;      Action:  Scrolls the window specified by the caller by
;               the number of lines passed in AL.  The blank
;               lines inserted at screen bottom are cleared
;               to blanks with 07H as the display attribute
;
;   VIDEO6:
;      Caller must pass:
;      CH: Y coordinate, upper left corner of window
;      CL: X coordinate, upper left corner of window
;      DH: Y coordinate, lower right corner of window
;      DL: X coordinate, lower right corner of window
;      AL: number of lines to scroll window by (0 clears it)
;      BH: display attribute for blanked lines (07H is "normal")
;      Action:  Generic access to BIOS VIDEO service 6.  Caller
;               must pass ALL register parameters as shown above
;---------------------------------------------------------------

ClrScr:
    sub esp, 16
    mov edi, esp
    mov byte [edi], 27
    inc edi
    mov byte [edi], '['
    inc edi
    mov byte [edi], '2'
    inc edi
    mov byte [edi], 'J'
    inc edi
    mov byte [edi], 0
    mov edx, esp
    call Write
    add esp, 16
    ret
   
           mov CX,0            ; Upper left corner of full screen
           mov DX,word [LRXY]  ; Load lower-right XY coordinates into DX
ClrWin:    mov AL,0            ; 0 specifies clear entire region
ScrlWin:   mov BH,07H          ; Specify "normal" attribute for blanked line(s)
VIDEO6:    mov AH,06H          ; Select VIDEO service 6: Initialize/Scroll
           int 10H             ; Call VIDEO
           ret                 ; Return to the caller


;---------------------------------------------------------------
;   WRITE    --  Displays information to the screen via DOS
;                service 9: Print String
;   Last update 9/12/99
;
;   1 entry point:
;
;   Write:
;      Caller must pass:
;      DS: The segment of the string to be displayed
;      DX: The offset of the string to be displayed
;          String must be terminated by "$"
;      Action:  Displays the string at DS:DX up to the "$" marker
;---------------------------------------------------------------

Write:
    mov ecx, edx
    or edx, byte -1
.getlen:
    inc edx
    cmp byte [ecx + edx], 0
    jnz .getlen
    mov ebx, 1
    mov eax, 4
    int 80h
    ret
   
;---------------------------------------------------------------
;   WRITELN  --  Displays information to the screen via DOS
;                service 9 and issues a newline
;   Last update 9/12/99
;
;   1 entry point:
;
;   Writeln:
;      Caller must pass:
;      DS: The segment of the string to be displayed
;      DX: The offset of the string to be displayed
;          String must be terminated by "$"
;      Action:  Displays the string at DS:DX up to the "$" marker
;               marker, then issues a newline.  Hardware cursor
;               will move to the left margin of the following
;               line.  If the display is to the bottom screen
;               line, the screen will scroll.
;      Calls: Write
;---------------------------------------------------------------

Writeln:
           call Write      ; Display the string proper through Write
           mov  eDX,CRLF    ; Load address of newline string to DS:DX
           call Write      ; Display the newline string through Write
           ret             ; Return to the caller

Later,
Frank


Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #3 on: February 18, 2016, 12:13:03 PM »
Thank you Frank for a very informative response.

The Linux system call 64-bit list, I got from the Linux Mint forum, leaves a tad to be desired.
Some functions are obvious by the label and some are not obvious.
Where does one get an explanation of each call?


From what I see, NASM seems to replace windows API commands with
special functions (MACROS) like GOTOXY etc.
Fine. One wonders where there is a complete 64-bit list of these functions?
Will  go back and review NASMX again.
« Last Edit: February 18, 2016, 12:18:56 PM by jackjps »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #4 on: February 18, 2016, 03:14:19 PM »
Hi jackjps,

I should make clear that I'm still running 32-bit and have very little experience with 64-bit. A long sad story. I'll cry on ya some other time.

Short answer - the system calls are in section 2 of the man pages. "man write" will give you the bash command, "man 2 write" will give you sys_write - or the "write()" wrapper for it. This is true for 32-bit, I'm pretty sure it's true of 64-bit. That may be the best that you can do. (it is in C terms, not asm terms)

NASMX has a lot of examples, but not much "documentation" as such. Rob Neff, who was maintaining NASMX, has just stepped down - "real life" interferes - so I don't know what's going to be happening with that...

Best,
Frank


Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #5 on: February 18, 2016, 09:47:53 PM »
Thank you again Frank.
I hate to ask this question but I have to.
Where can I find the "man Pages"?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #6 on: February 19, 2016, 12:17:33 AM »
Ahhh... well.... hmmmm... I am embarrased to admit I don't know where the files are actually located. They're just "there". It's your "help system". /usr/man/ apparently. Linux differentiates between left and right alt and control keys - pressing left alt-control and some function key will get you into a console. (alt F7 will get you back into the GUI - you'll want to know that! :) )

Apparently, some distros don't have a "log in shell". You're just in the console, I guess. What I'm familiar with, and what I think you'll find, you have to log in (and give the password, of course). At that point, you can just type "man 2 write" (or some system call you don't know what it does) and get a description of what it does, what the parameters are, what it returns, and what the possible errors are. The man pages will claim that it returns -1 if error (usually) and the error number is in errno. If we call the system calls directly, the negative of the error number is in rax (eax for me). I ASSume there's a way to do all this from the GUI, but I don't know what it is. :)

Best,
Frank


Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #7 on: February 19, 2016, 11:51:07 AM »
Hi Frank,
The instruction "man 2 write" works very well.
When in that hitting "h" gives a help screen with more info than
you want(mind boggling).

Here is a url to the Man Pages: http://kernel.org/doc/man-pages.
There is a ton of info there, that I have no idea how it applies to
assembler coding. As I don't do "C" why would I need this????

A suggestion if I may. You really need to be coding in 64-bit
as 32-bit is rapidly becoming obsolete.
« Last Edit: February 19, 2016, 11:58:46 AM by jackjps »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #8 on: February 19, 2016, 04:17:32 PM »
What could I code in 64-bit that I can't code in 32-bit? In what sense is it "obsolete"?

Best,
Frank


Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #9 on: February 19, 2016, 08:02:14 PM »
HI Frank,
I am assuming your have a 32-bit computer and running a 32-bit OS.
That's all fine and good. But you will find that running a 64-bit computer
with a 64-bit OS runs much faster, 64-bit architecture has been around
over 10 years and 32-bit is like running 16 bit when 32-bit is out.
Yes, you can run 32-bit programs on a 64-bit computer but it's not the
best way to do it IMHO.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #10 on: February 20, 2016, 02:51:18 PM »
You may be right.
Quote
But you will find that running a 64-bit computer
with a 64-bit OS runs much faster,
Do you have evidence of this, or is it an assumption? True of all programs, or just certain programs? What would make a good test case?
I am of the opinion that 64-bit code will run slower, other things being equal. It is, at this point, just my opinion.

Best,
Frank


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #11 on: February 20, 2016, 05:19:15 PM »
jackjps,

On the thread that Frank linked you too earlier, I posted a "one-liner" command line that can be used to generate a header containing all the 64-bit system call numbers to their associated names. This command requires that you have glibc header collection installed from GCC.

Code: (Linux Console) [Select]
grep __NR $(locate unistd_64.h) | grep define | sed -e 's/\#/\%/' -e 's/__NR_/SYS_/' > unistd_64.inc
Note: the above one liner has changed slightly from the last version. Apparently GCC moved the header so we'll let the system find the file for us to make it more portable between versions.

The list that's generated should have each system call renamed using NASM's %define directive to it's symbolic counterpart. If you want to learn more about each specific system call, open that file and copy the name of the system call without the SYS_ part and use the man (or xman) utility to find information on that system call. An example session at your command prompt would look like this..

Code: [Select]
$ grep __NR $(locate unistd_64.h) | grep define | sed -e 's/\#/\%/' -e 's/__NR_/SYS_/' > unistd_64.inc
$ ls -a
.  ..  unistd_64.inc
$ head unistd_64.inc
%define SYS_read 0
%define SYS_write 1
%define SYS_open 2
%define SYS_close 3
%define SYS_stat 4
%define SYS_fstat 5
%define SYS_lstat 6
%define SYS_poll 7
%define SYS_lseek 8
%define SYS_mmap 9
$ man mmap

So, for someone new to Linux, a description of the above might be in order. The first command creates the file using a lot of "command line magic", you'll only have to do it once so don't get to focused on how it works right now (that's something fun for later). The second command (ls -a) lists all files and directories in your current working directory. In this case I was in a sub-directory that just had the unistd_64.inc that the first command created. The head command shows only the first few lines of a file. I used it as an example. If you want to scroll through the file, try using the less command instead of head. Next, I find a system call that I'm interested in. In this case I decided I wanted to know more about the SYS_mmap system call. As we can see, it is system call number 9... however, we don't know what parameters or what type of parameters it takes. This is where the man command comes in. Below is the output of the man mmap command executed below.

Code: (man mmap) [Select]
MMAP(2)                    Linux Programmer's Manual                   MMAP(2)

NAME
       mmap, munmap - map or unmap files or devices into memory

SYNOPSIS
       #include <sys/mman.h>

       void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);
       int munmap(void *addr, size_t length);

       See NOTES for information on feature test macro requirements.

DESCRIPTION
       mmap()  creates a new mapping in the virtual address space of the call?
       ing process.  The starting address for the new mapping is specified  in
       addr.  The length argument specifies the length of the mapping.

       If addr is NULL, then the kernel chooses the address at which to create
       the mapping; this is the most portable method of creating  a  new  map?
       ping.   If  addr  is not NULL, then the kernel takes it as a hint about
       where to place the mapping; on Linux, the mapping will be created at  a
 Manual page mmap(2) line 1 (press h for help or q to quit)

In the manpages you can get an understanding of the parameters used by the routine, what error codes are returned and how they should be dealt with, and usually some program examples (written in C) showing how the system call is used.

Also note the name of the manual page is "mmap(2)". All system calls are in 2 and you can specify that on the command line. For example, if you were to use man stat you would notice that you get the output of the User Command stat(1) and this isn't what you want. To get to the system call, explicitly specify 2 on the command line with man 2 stat.

Earlier, I mentioned something called xman. This is a manual page viewer that comes with the X windows environment. This is a great tool to use when your exploring the various features of Linux. xman lets you browse through the documentation on your system using a graphical interface.

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #12 on: February 21, 2016, 01:04:30 AM »
To Frank,
It's my assumption. but it stand to reason that grabbing 4 bytes
has to be faster than grabbing 2 bytes. Don't take my word for it
but check it out on the internet.

To Bryant,
I'm working on understanding your post.
I'm afraid most of it is over my head, but
will give it a shot. Thank you.
 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #13 on: February 21, 2016, 04:52:22 AM »
Trying to convince me to not be such a luddite is probably not the best use of your time. We can discuss why "-f elfx32" exists as we go along. If you know a URL where I can download a clot of ambition... I have a 64-bit machine at my feet (my daughter's old machine)... I didn't say I had "no" experience with 64-bit code, only "very little". I installed "the wrong distro for me" on it. I shouldn't name the distro, since I have nothing nice to say about it, but... well... it was Mint. If it works for you, that's fine, but I need to resystem first thing. Not today... probably not tomorrow either...

Bryant just gave you the 64-bit syscall numbers and suggested "xman" to view the man pages from what I call the "cartoon interface". It isn't as complicated as it looks.

Best,
Frank

P.S. I agree about "captcha" but I don't know how to fix it. I think - I hope - that it'll go away and leave you alone soon.

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #14 on: February 21, 2016, 03:22:52 PM »
                  2-21-16
I hate to ask this basic stuff but I must.
Appreciate all your help.

 Questions about Linux and Nasm.
 1. don't know if I have 'glibc' or how to get it if I don't
    or what is does? What is GCC?
       I found glibc and installed it......
 2. what is VT100? I go back to the DOS days and card input
    and wiring boards. Glad that has come & gone.
 3. How do I program to use ex: 'gotoxy'?   
    I tried to use it and it failed because I don't know
    how to use it.
 4. What is the "BASH' command?
 5. What is 'xman'? Do I already have it? If so, where is it?
 6. do all commands (ex: write) use this sequence or are their
    variations:
    rax  for the system call
    rdi  for the output
    rsi  message length
    rdx  message length?
 7. in 'clrscr' What is Video6? Do I have it?
 8. if I do a man 2 pause, it says that I need to include
    '#include <unistd.h>'. When I add this to my program it fails

   
« Last Edit: February 21, 2016, 04:31:52 PM by jackjps »