HI Bryant,
1. The following pretty much lost me.
EX: I read the "man chmod" and didn't understand it.
So I am not able to fill this out "chmod +x demo64.sh".
2. The next example. Am I supposed to fill out those fields or just
use it as is???
Which one should I use?
3.
I need to make sure that the user only enters
digits in demo64. I read the man pages on these two calls
and again I didn't understand the explanation. How do you
know what to put where? Have no idea how to enter the x/y
coordinates for placing a message.
;------------------------------------------------
; 3-1-2016 using NASM 64
; Order of colors: BGR
;
; Bryants goodies
; Use Case:
; - create a full screen of a light blue color.
; - put a small rectangle on the screen in yellow.
; - let the user enter 4 digits in black in that rectangle.
; - read the 4 digits from the rectangle.
; Dependencies:
; libc-dev - GNU C library.
; libncurses-dev - Developer's libraries for ncurses.
; Build Process:
; nasm -f elf64 demo64.asm -o demo64.o
; gcc -o demo64 demo64.o -lcurses
; Run Process:
; ./demo64
bits 64
cpu IA64
global main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions from the Standard C Library
extern signal
extern atoi
extern exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Functions from the NCurses Library
extern initscr
extern keypad
extern nonl
extern cbreak
extern echo
extern has_colors
extern start_color
extern init_pair
extern newwin
extern endwin
extern refresh
extern waddstr
extern wattron
extern wrefresh
extern wgetch
extern wgetnstr
extern wbkgd
extern LINES
extern COLS
%define SIGINT 2 ; defined in signal.h
%define TRUE 1 ; defined in curses.h
%define FALSE 0 ; defined in curses.h
%define STDERR 2
%define COLOR_BLACK 0
%define COLOR_RED 1
%define COLOR_GREEN 2
%define COLOR_YELLOW 3
%define COLOR_BLUE 4
%define COLOR_MAGENTA 5
%define COLOR_CYAN 6
%define COLOR_WHITE 7
%define COLOR_PAIR(n) ((n) << (8))
%define BLACK_ON_BLUE 1
%define BLACK_ON_YELLOW 2
%define BLACK_ON_BLUE 1
%define BLACK_ON_YELLOW 2
%define STACK_ALIGN sub rsp, 8
%define STACK_RESTORE add rsp, 8
section .data
hWindow: dq 0
hChildWindow: dq 0
szUserInput: db 0, 0, 0, 0, 0
uinputerr db "only digits may be entered - enter again",0
section .text
; global _start ;?? bryant said these are included in the "c" lib
;_start: ;??
;; do_init : nothing -> boolean .
;; preform all of the applications initialization.
;; when initialization was successful, hWindow will
;; contain the curses screen handle and the return
;; value will be TRUE. otherwise, it returns FALSE.
do_init:
STACK_ALIGN
;; setup an interrupt to terminate
mov rsi,do_exit
mov rdi,SIGINT
call signal
;; initialize the curses library
call initscr
;; was curses initialization successful?
cmp rax,0
jne .CursesInitSuccessful
;; initialization failed, return FALSE
mov rax,FALSE
ret
.CursesInitSuccessful:
;; save a copy of curses root window
mov [hWindow],rax
;; enable keyboard mapping
mov rsi,TRUE
mov rdi,[hWindow]
call keypad
;; tell curses not to translate LF into CRLF on output
call nonl
;; take input chars one at a time, no waiting for newline
call cbreak
;; enable echoing of input
call echo
;; does this system support color terminals?
call has_colors
cmp rax,0
jne .HasColorSupport
;; No color support, return FALSE
STACK_RESTORE
mov rax,FALSE
ret
.HasColorSupport:
call start_color
;; create our color pairs
;; black foreground, blue background
mov rdx,COLOR_BLUE
mov rsi,COLOR_BLACK
mov rdi,BLACK_ON_BLUE
call init_pair
;; black foreground, yellow background
mov rdx,COLOR_YELLOW
mov rsi,COLOR_BLACK
mov rdi,BLACK_ON_YELLOW
call init_pair
;; lets create a child window
mov rcx,50 ; x value
mov rdx,10 ; y value
mov rsi,5 ; 5 columns for 4 digits
mov rdi,1 ; 1 line for 1 row of digits
call newwin
mov [hChildWindow],rax
;; if we reach this point, we have initialized successfully.
STACK_RESTORE
mov rax,TRUE
ret
;; do_exit : integer -> nothing .
;; exit to the operating system.
do_exit:
STACK_ALIGN
;; refresh our screen
call refresh
;; shut down curses
call endwin
;; return to operating system
mov rdi,0
call exit
;; we should never reach this point
STACK_RESTORE
ret
;; clear_blue : nothing -> nothing .
;; create a full screen of a light blue color.
clear_blue:
STACK_ALIGN
;; make our background blue
mov rsi,COLOR_PAIR(BLACK_ON_BLUE)
mov rdi,[hWindow]
call wbkgd
;; refresh our screen
mov rdi, [hWindow]
call wrefresh
STACK_RESTORE
ret
;; draw_child_window : nothing -> nothing .
;; put a small rectangle on the screen in yellow.
draw_child_window:
STACK_ALIGN
;; make our background yellow
mov rsi,COLOR_PAIR(BLACK_ON_YELLOW)
mov rdi,[hChildWindow]
call wbkgd
;; refresh our child window
mov rdi,[hChildWindow]
call wrefresh
STACK_RESTORE
ret
;; get_user_input : nothing -> integer.
;; let the user enter 4 digits in black in the rectangle.
get_user_input:
STACK_ALIGN
;; get user input
;; here I am trying to test the input from the user for valid
;; digits. The error message should go on the same line as the
;; yellow rectangle. This bad code gives the error message at
;; x-0,y-0. The yellow rectangle is not cleared for new input.
.n1:
mov rdx,4
mov rsi,szUserInput
mov rdi,[hChildWindow]
call wgetnstr ; reads all 4 characters good or bad
mov rsi,szUserInput ; check user input for digits
mov ah,3
.n2:
cmp ah,3 ; process 4 digits
jg .out1 ; good input
mov al,[rsi]
cmp al,0
jl .err1
cmp al,9
jg .err1
inc ah
inc rsi
jmp .n2
.err1:
;; Display error message in black text on a yellow background.
mov rsi,COLOR_PAIR(BLACK_ON_YELLOW)
mov rcx,5 ;???? x value
mov rdx,10 ;????? y value
mov rdi, [hWindow]
call wattron
;uinputerr db "only digits may be entered - start over",0
mov rsi,uinputerr
mov rdi, [hWindow]
call waddstr
;; The last two commands wrote a string on the window buffer,
;; now we need to display that buffer on the screen.
mov rdi, [hWindow]
call wrefresh
call draw_child_window
jmp .n1
.out1:
;; convert string to integer
;; rdi, rsi, rdx, rcx, r8, r9
mov rdi,szUserInput
call atoi
STACK_RESTORE
ret
;; main : integer (string . string) -> integer .
;; our program starts here.
main:
STACK_ALIGN
call do_init
cmp rax,TRUE
jne .done
;; clear the screen to blue
call clear_blue
;; draw yellow window
call draw_child_window
;; get user input
call get_user_input
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; At this point in our code, the number we ;;
;; entered into our child window is in the ;;
;; RAX register. We can now use it for any- ;;
;; thing we want. ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.done:
STACK_RESTORE
xor rdi,rdi
call do_exit
ret