Author Topic: beginner questions  (Read 83070 times)

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #60 on: March 06, 2016, 02:42:08 PM »
          3-6-2016
Hi Bryant,
A few more of my dumb questions Please.
Having trouble editing the input data from the user.
It's my lack of knowledge of NASM.

hWindow:       dq 0
hChildWindow:  dq 0
szUserInput:   db 0, 0, 0, 0, 0
Why the colon after items in the Data section????

You defined szUserInput as follows:
  szUserInput: db 0,0,0,0,0
I am used to something like
  szUserInput db '    ',0
  If the user enters this data '1234'
  is it coded as 31,32,33,34 OR
  as f1,f2,f3,f4?

All the input fields must be digits.
1st two can not be greater than 22.
2nd two can not be greater than 08.

Is there such a thing as a "struct" in NASM?
How do you reference each byte in NASM?
 can it be done this way:
  xor rsi,rsi
.t1:
  mov ch,byte [szUserInput+rsi]
  inc rsi
  jmp .t1 etc......
 Thanks
 
« Last Edit: March 06, 2016, 09:51:25 PM by jackjps »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #61 on: March 07, 2016, 04:30:08 AM »
Hi jackjps,

I can answer some of this...

Quote

hWindow:       dq 0
hChildWindow:  dq 0
szUserInput:   db 0, 0, 0, 0, 0
Why the colon after items in the Data section????
Optional, after a label defining a variable. If the label is the only thing on the line. Nasm will generate a suppressible warning.

Quote
  If the user enters this data '1234'
  is it coded as 31,32,33,34
31h, 32h, 33h, 34h

Quote
All the input fields must be digits.
1st two can not be greater than 22.
2nd two can not be greater than 08.
You'll have to check those "by hand".

Nasm knows "struct", but spells it "struc". See:
http://www.nasm.us/doc/nasmdoc4.html#section-4.12.10
Note that "struc" only defines a structure, does not allocate any memory. Use "istruc" to allocate an instance (if you need to). The NASMX package includes "xstruc", which is somewhat more flexible, allowing nested structures and unions.

Code: [Select]
  xor rsi,rsi
.t1:
  mov ch,byte [szUserInput+rsi]
  inc rsi
  jmp .t1
that's fine, or as you had it before the edit:
Code: [Select]
$define B byte

  xor rsi,rsi
.t1:
  mov ch,B[szUserInput+rsi]
  inc rsi
  jmp .t1
Since the register cl determines the size of the operation, we don't really need to say "byte" at all. May make it clearer for us humans, but Nasm knows. :)

Best,
Frank




Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #62 on: March 07, 2016, 02:21:02 PM »
Thanks Frank for responding,
I need a way to view all the 64-bit registers
without going into any debug stuff I know nothing about.
Does ncurses help any with that?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: beginner questions
« Reply #63 on: March 07, 2016, 04:43:10 PM »
I think the correct answer to this is to learn  gdb. I don't "like" gdb much, so I haven't learned it very well, so I don't "like" it much. This is not a good plan!

However, you can display a 64-bit register the same as displaying any other number. That is, "just call printf" or do it in real assembly language. I presume you'll want it in hex. All the 64-bit registers is going to make quite a screenful of numbers. I think "ncurses" will offer some tricks to arrange it all neatly on the screen, but I doubt if it has printf-like capabilities. I dunno, you're the one who's reading the ncurses manual. That library from Soffian surely has such a routine. Even though I haven't done it, my advice is to learn to run gdb.

Best,
Frank


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #64 on: March 10, 2016, 01:14:53 AM »
I agree with Frank. GDB is the way to go, it's text based but it's the most effective debugger on linux/bsd systems. If you absolutely *need* a GUI for your debugger, GNU's DDD program is nice, but has an even steeper learning curve than GDB itself. I used to like Insight debugger, but for some reason it was removed from the debian repositories and I'm too lazy to download and build it from the developer's website.  :-[

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #65 on: March 13, 2016, 03:07:41 PM »
I like to keep my program files separate from any OS.
OSes: Linux mint 17.3 rosa excp 64-bit and windows 7 pro 64-bit.
These OSes are on a separate SSD drive.
I do this on a 1T HD.  EX: masm32, GoAsm and now NASM.
If the unexpected happens my program files are safe.
I also back up these files regularly.

I can execute the masm32 and GoAsm program from the HD
with no problems...
Today I tried to do the same thing with my demo64 NASM program.
This is what happened:  "bash ./demo64: permission denied".
Thanks for any help...
« Last Edit: March 13, 2016, 03:23:55 PM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #66 on: March 13, 2016, 11:10:05 PM »
I like to keep my program files separate from any OS.
OSes: Linux mint 17.3 rosa excp 64-bit and windows 7 pro 64-bit.
These OSes are on a separate SSD drive.
I do this on a 1T HD.  EX: masm32, GoAsm and now NASM.
If the unexpected happens my program files are safe.
I also back up these files regularly.

I can execute the masm32 and GoAsm program from the HD
with no problems...
Today I tried to do the same thing with my demo64 NASM program.
This is what happened:  "bash ./demo64: permission denied".
Thanks for any help...

How comfortable are you with Linux configuration files? Sounds to me like your 1T HD is being mounted with the noexec bit. You should be able to change "noexec" to "exec" in the /etc/fstab file, but I'd hate for you to accidentally cripple your system by editing the wrong one.

As a temporary solution, from the shell prompt, you can run sudo mount -o remount,exec /path/to/partition where /path/to/partition is the actual location of your partition. After doing that, you should try to use the ./demo64 command again and if it runs, that's your problem. You'll have to read up on editing the /etc/fstab file (or possibly just use that above command every time you want to run something from that drive)

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #67 on: March 14, 2016, 12:49:24 AM »
HI Bryant,
This what I got when I ran what you suggested:
jack@jack-myheadache ~ $ sudo mount -o remount,exec /media/jack/nasm/nasmfld
[sudo] password for jack:
mount: can't find /media/jack/nasm/nasmfld in /etc/fstab or /etc/mtab
jack@jack-myheadache ~ $

I have no idea where the /media came from but when I go to the place where the
./demo64 program is located on the HD, that is the address I see.
I really need to do this your other way as I would like to work off the HD if I can.
But you are right, I can't take the chance at my learning level and mess up
the computer.
« Last Edit: March 14, 2016, 03:20:07 AM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #68 on: March 15, 2016, 08:43:15 AM »
The /media directory is where your partitions are mounted. Instead of that full path, try sudo mount -o remount,exec /media/jack

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #69 on: March 15, 2016, 10:23:36 AM »
Code: [Select]
Thanks for responding Bryant,
This is what I got.....

jack@jack-myheadache ~ $ sudo mount -o remount,exec /media/jack
[sudo] password for jack:
mount: can't find /media/jack in /etc/fstab or /etc/mtab
jack@jack-myheadache ~ $
;***************************************************************
Generally I want the whole screen to be Blue and it is.
All heading messages are in White and it is.
I want any error messages to be in say Yellow.
So the problem is how to make just the error messages Yellow
and leave the headings etc. in White? I tried adding an init_pair
Yellow/Blue and all the screen messages became Yellow.
I tried adding the init_pair where I wrote the error message
and it still made everything yellow.
Hope this makes sense....
« Last Edit: March 15, 2016, 08:30:02 PM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #70 on: March 16, 2016, 11:44:39 PM »
Generally I want the whole screen to be Blue and it is.
All heading messages are in White and it is.
I want any error messages to be in say Yellow.
So the problem is how to make just the error messages Yellow
and leave the headings etc. in White? I tried adding an init_pair
Yellow/Blue and all the screen messages became Yellow.
I tried adding the init_pair where I wrote the error message
and it still made everything yellow.
Hope this makes sense....

The init_pair creates a new color index, the problem is we use wbkgd to set the color attribute for the whole window (which is usually how we do this). However, as you've discovered, there are times you want different color text on the same window.  :)

Luckily, NCurses has a function for that too. It's called, wchgat and the manual page on it is kinda cryptic. What you'll need to do is use the get_cur_coord macro just before you write your error message to the screen, and make sure you save the coordinates. After you write the string, use wmove to return to the coordinates you just saved. Once you're at the beginning of the string you want to make a different color, you then call wchgat to change the color and attributes of the string you just wrote.

Code: (int wchgat(WINDOW *win, int n, attr_t attr, short color, const void *opts)) [Select]
mov r8, 0               ; curses requires the "opts" parameter for future features that aren't currently implemented. so we just set it to 0.
mov rcx, YELLOW_ON_BLUE ; or whatever you named the color pair index using init_color
mov rdx, 0              ; the string attribute; 0 for normal, and 200000h for bold
mov rsi, szMessage_len  ; the length of the error message you printed.
mov rdi, [hWindow]      ; the window your string was printed on.
call wchgat

If you can't get it working with the above, post what you've got so far and I'll try to help. :)

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #71 on: March 17, 2016, 01:15:25 PM »
Code: [Select]
            3-17-2016
Hi Bryant,
This is what I got from my code below:
#1...........
jack@jack-myheadache ~/nasmfld $ nasm -f elf64 demo64.asm -o demo64.o
jack@jack-myheadache ~/nasmfld $ gcc -o demo64 demo64.o -lcurses
demo64.o: In function `get_user_input.err1':
demo64.asm:(.text+0x4b8): undefined reference to `get_cur_coord'
collect2: error: ld returned 1 exit status
jack@jack-myheadache ~/nasmfld $

   extern get_cur_coord    ; have no idea how or where to define this
   extern wchgat
   %define BLACK_ON_YELLOW 2

   call get_cur_coord
   mov r8, 0        ; curses requires the "opts" parameter for future features
                    ; that aren't currently implemented. so we just set it to 0.
   mov rcx, BLACK_ON_YELLOW ; or whatever you named the color pair index using
                    ; init_color
   mov rdx, 0       ; the string attribute; 0 for normal, and 200000h for bold
   mov rsi,42       ; the length of the error message you printed.
   mov rdi, [hWindow]      ; the window your string was printed on.
   call wchgat

;  call draw_child_window3
   mov rdx,50    ;  X
   mov rsi,11    ;  Y
;   mov rdi,[hChildWindow3]
   mov rdi,[hWindow]
   call wmove
;*****************

#2................
also never got this to work.
trying to run NASM from a HD not containing the OS.
jack@jack-myheadache ~ $ sudo mount -o remount,exec /media/jack/nasm/nasmfld
[sudo] password for jack:
mount: can't find /media/jack/nasm/nasmfld in /etc/fstab or /etc/mtab
jack@jack-myheadache ~ $
; *****************

#3...................
In MASM32 we have macro called "ATODW".
It converts a decimal string into a dw value.
CkBal db  '     4321',0
mathhold dd 0
I thought this was what "atoi" did....
more about this problem below.

; the user keys in "aa" incorrectly

hChildWindow   dq 0
msg                   dw 0   ; converted user input ??
szUserInput       db '  ',0

   mov rdx,2
   mov rsi,szUserInput
   mov rdi,[hChildWindow]
   call wgetnstr      ; reads 2 characters good or bad

;;   convert string to integer
   mov rdi,szUserInput
   call atoi   ; result in rax

   mov [msg],ax     ; preserve keyed in value ??
   cmp byte[msg+1],016h  ; This does not work!!!!!!!!!
                         ; value should not be > 22 decimal
    jg .err1
    jmp .out1               ; good input

; never gets here.............
.err1:
   call draw_child_window3
   mov rdx,50    ;  X
   mov rsi,11    ;  Y
   mov rdi,[hChildWindow3]
   call wmove
;msg6       db 'value keyed in is invalid - reenter ',0
   mov rsi,msg6
   mov rdi,[hChildWindow3]
   call waddstr

;;   The command wrote a string in the window buffer,
;;   now we need to display the buffer on the screen.
   mov rdi,[hChildWindow3]
   call wrefresh

« Last Edit: March 17, 2016, 09:37:57 PM by jackjps »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #72 on: March 17, 2016, 11:03:49 PM »
            3-17-2016
Hi Bryant,
This is what I got from my code below:
Code: [Select]
jack@jack-myheadache ~/nasmfld $ nasm -f elf64 demo64.asm -o demo64.o
jack@jack-myheadache ~/nasmfld $ gcc -o demo64 demo64.o -lcurses
demo64.o: In function `get_user_input.err1':
demo64.asm:(.text+0x4b8): undefined reference to `get_cur_coord'
collect2: error: ld returned 1 exit status
jack@jack-myheadache ~/nasmfld $

get_cur_coord isn't a procedure you call, it's a macro that I gave you earlier in the thread.

Code: [Select]
   extern get_cur_coord    ; have no idea how or where to define this
   extern wchgat
   %define BLACK_ON_YELLOW 2

   call get_cur_coord
   mov r8, 0        ; curses requires the "opts" parameter for future features
                    ; that aren't currently implemented. so we just set it to 0.
   mov rcx, BLACK_ON_YELLOW ; or whatever you named the color pair index using
                    ; init_color
   mov rdx, 0       ; the string attribute; 0 for normal, and 200000h for bold
   mov rsi,42       ; the length of the error message you printed.
   mov rdi, [hWindow]      ; the window your string was printed on.
   call wchgat

;  call draw_child_window3
   mov rdx,50    ;  X
   mov rsi,11    ;  Y
;   mov rdi,[hChildWindow3]
   mov rdi,[hWindow]
   call wmove
;*****************

The use-case for writing an error message would be:
1. Save the cursor position using the get_cur_coord macro.
2. Write your error message using the waddstr procedure.
3. Restore cursor position using the wmove procedure.
4. Change attributes from current position to n-bytes, where n is the length of your error message, using the wchgat procedure.

You should try to implement this as a procedure.

also never got this to work.
trying to run NASM from a HD not containing the OS.
Code: [Select]
jack@jack-myheadache ~ $ sudo mount -o remount,exec /media/jack/nasm/nasmfld
[sudo] password for jack:
mount: can't find /media/jack/nasm/nasmfld in /etc/fstab or /etc/mtab
jack@jack-myheadache ~ $

Maybe you can ask for help on that at the Linux Mint community forums. They might be able to walk you through editing /etc/fstab to enable exec on your partition. I'd rather not try and talk you through it when i don't have a Linux Mint (or a 64-bit Linux) at my disposal for testing locally.

In MASM32 we have macro called "ATODW".
It converts a decimal string into a dw value.
CkBal db  '     4321',0
mathhold dd 0
I thought this was what "atoi" did....
more about this problem below.
Code: [Select]
; the user keys in "aa" incorrectly

hChildWindow   dq 0
msg                   dw 0   ; converted user input ??
szUserInput       db '  ',0

   mov rdx,2
   mov rsi,szUserInput
   mov rdi,[hChildWindow]
   call wgetnstr      ; reads 2 characters good or bad

;;   convert string to integer
   mov rdi,szUserInput
   call atoi   ; result in rax

   mov [msg],ax     ; preserve keyed in value ??
   cmp byte[msg+1],016h  ; This does not work!!!!!!!!!
                         ; value should not be > 22 decimal
    jg .err1
    jmp .out1               ; good input

; never gets here.............
.err1:
   call draw_child_window3
   mov rdx,50    ;  X
   mov rsi,11    ;  Y
   mov rdi,[hChildWindow3]
   call wmove
;msg6       db 'value keyed in is invalid - reenter ',0
   mov rsi,msg6
   mov rdi,[hChildWindow3]
   call waddstr

;;   The command wrote a string in the window buffer,
;;   now we need to display the buffer on the screen.
   mov rdi,[hChildWindow3]
   call wrefresh

The atoi procedure isn't exactly like ATODW from MASM32 package. atoi will convert a string of characters that representing numbers into an integer. However, it stops it's conversion if it runs into anything else. In your "aa" example, atoi is returning 0 because it didn't get a value from the string. If you were to pass atoi the string "55x2", atoi would return 55 and ignore the rest. If you want to ensure the string entered contains only digits, you'll need to check each character from the szUserInput buffer to ensure they are all digits. Since you're working with just two characters, this shouldn't be too hard. You could even do it inline (no loop) like this:

Code: [Select]
   mov al, byte [szUserInput]
   cmp al, '0'
   jb .err1
   cmp al, '9'
   ja .err1

   mov al, byte [szUserInput+1]
   cmp al, '0'
   jb .err1
   cmp al, '9'
   ja .err1
   jmp .out1

.err1:
   ;; one of the characters wasn't a decimal digit,
   ;; do our error for invalid input then jump to end
   ;; of procedure.

.out1:
   ;; both characters read are decimal digits, convert
   ;; them to integers.
   mov rdi, szUserInput
   call atoi
« Last Edit: March 17, 2016, 11:06:45 PM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline jackjps

  • Jr. Member
  • *
  • Posts: 60
Re: beginner questions
« Reply #73 on: March 18, 2016, 06:43:10 PM »
      3-18-2015
Just Info.
Much to my surprise and using input from the user, "szUserInput"
codes a letter as 061h.
The number 0 is coded as 030h. This is without using "atoi".
Puzzling....

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: beginner questions
« Reply #74 on: March 18, 2016, 07:13:20 PM »
Your computer doesn't understand concepts such as pictures, sounds, or even letters. All the computer understands are numbers. But this isn't good enough for us humans, we like pictures, sounds, and letters.. even more so, we like combinations of pictures (video), sounds (music), and letters (words). So internally, computers tend to represent these more abstract things that people like, as numbers. In this case, you're introduced to something known as ASCII code. ASCII code is how your computer is storing letters based on the context in which they are used. Here is a full list of the ASCII code table:

Code: [Select]
Char  Dec  Oct  Hex | Char  Dec  Oct  Hex | Char  Dec  Oct  Hex | Char Dec  Oct   Hex
-------------------------------------------------------------------------------------
(nul)   0 0000 0x00 | (sp)   32 0040 0x20 | @      64 0100 0x40 | `      96 0140 0x60
(soh)   1 0001 0x01 | !      33 0041 0x21 | A      65 0101 0x41 | a      97 0141 0x61
(stx)   2 0002 0x02 | "      34 0042 0x22 | B      66 0102 0x42 | b      98 0142 0x62
(etx)   3 0003 0x03 | #      35 0043 0x23 | C      67 0103 0x43 | c      99 0143 0x63
(eot)   4 0004 0x04 | $      36 0044 0x24 | D      68 0104 0x44 | d     100 0144 0x64
(enq)   5 0005 0x05 | %      37 0045 0x25 | E      69 0105 0x45 | e     101 0145 0x65
(ack)   6 0006 0x06 | &      38 0046 0x26 | F      70 0106 0x46 | f     102 0146 0x66
(bel)   7 0007 0x07 | '      39 0047 0x27 | G      71 0107 0x47 | g     103 0147 0x67
(bs)    8 0010 0x08 | (      40 0050 0x28 | H      72 0110 0x48 | h     104 0150 0x68
(ht)    9 0011 0x09 | )      41 0051 0x29 | I      73 0111 0x49 | i     105 0151 0x69
(nl)   10 0012 0x0a | *      42 0052 0x2a | J      74 0112 0x4a | j     106 0152 0x6a
(vt)   11 0013 0x0b | +      43 0053 0x2b | K      75 0113 0x4b | k     107 0153 0x6b
(np)   12 0014 0x0c | ,      44 0054 0x2c | L      76 0114 0x4c | l     108 0154 0x6c
(cr)   13 0015 0x0d | -      45 0055 0x2d | M      77 0115 0x4d | m     109 0155 0x6d
(so)   14 0016 0x0e | .      46 0056 0x2e | N      78 0116 0x4e | n     110 0156 0x6e
(si)   15 0017 0x0f | /      47 0057 0x2f | O      79 0117 0x4f | o     111 0157 0x6f
(dle)  16 0020 0x10 | 0      48 0060 0x30 | P      80 0120 0x50 | p     112 0160 0x70
(dc1)  17 0021 0x11 | 1      49 0061 0x31 | Q      81 0121 0x51 | q     113 0161 0x71
(dc2)  18 0022 0x12 | 2      50 0062 0x32 | R      82 0122 0x52 | r     114 0162 0x72
(dc3)  19 0023 0x13 | 3      51 0063 0x33 | S      83 0123 0x53 | s     115 0163 0x73
(dc4)  20 0024 0x14 | 4      52 0064 0x34 | T      84 0124 0x54 | t     116 0164 0x74
(nak)  21 0025 0x15 | 5      53 0065 0x35 | U      85 0125 0x55 | u     117 0165 0x75
(syn)  22 0026 0x16 | 6      54 0066 0x36 | V      86 0126 0x56 | v     118 0166 0x76
(etb)  23 0027 0x17 | 7      55 0067 0x37 | W      87 0127 0x57 | w     119 0167 0x77
(can)  24 0030 0x18 | 8      56 0070 0x38 | X      88 0130 0x58 | x     120 0170 0x78
(em)   25 0031 0x19 | 9      57 0071 0x39 | Y      89 0131 0x59 | y     121 0171 0x79
(sub)  26 0032 0x1a | :      58 0072 0x3a | Z      90 0132 0x5a | z     122 0172 0x7a
(esc)  27 0033 0x1b | ;      59 0073 0x3b | [      91 0133 0x5b | {     123 0173 0x7b
(fs)   28 0034 0x1c | <      60 0074 0x3c | \      92 0134 0x5c | |     124 0174 0x7c
(gs)   29 0035 0x1d | =      61 0075 0x3d | ]      93 0135 0x5d | }     125 0175 0x7d
(rs)   30 0036 0x1e | >      62 0076 0x3e | ^      94 0136 0x5e | ~     126 0176 0x7e
(us)   31 0037 0x1f | ?      63 0077 0x3f | _      95 0137 0x5f | (del) 127 0177 0x7f

As you can see, it expects that the hex value 0x61 is translated as the letter 'a'. This ASCII standard allows for all applications that use letters as data, to store them in the same way.

About Bryant Keller
bkeller@about.me