This question already has an answer here:
%include "along64.inc"
default rel
section .data
EnterMsg : db "Enter a number to add to the list; enter 0 when you done" ,0ah,0
ExitMsg : db "The sorted list: " ,0ah,0
InputError1 : db "Input Error; Integers Only" ,0ah,0
InputError2 : db "Input Error; Maximum list is 100" ,0ah,0
InputError3 : db "Input Error; Enter AT LEAST ONE Int" ,0ah,0
segment .bss
a: resq 100 ;creates a new array with 100 values set to 0
section .text
global main
main:
mov rbx,0
mov rcx,0 ;sets counter to 0
mov rdx, EnterMsg ;moves EnterMsg to rdx to be called by WriteString
call WriteString ;prints EnterMsg
jmp read ;calls readInput label
read: ;reads the input
call ReadInt ;reads integer
jo invalidInput ;jumps if not an integer value
cmp rcx, 100 ;compares rcx and 100
jg tooManyInts ;if rcx is already 100, then cannot add int to array, so jump to error
cmp rax,0 ;tests for the 0 input
je Loop1 ;jumps to Loop1 if zero
mov [a +(rcx*4)], rax ;adds read integer to array
inc rcx ;increments counter
jmp read ;loops if input is not 0
Loop1:
cmp rcx, 2 ;compares rcx to 2
jmp badInput ;jumps to badinput if less than 2
push rcx ;pushes rcx for number of numbers to print
dec rcx ;decrements rcx because 0-based indexing
Loop2:
push rcx ;saves outer loop count
mov rbx, 0 ;sets rbx to 0 because 0 based indexing
Loop3:
mov rax,qword[a +(rbx * 4)] ;moves current value of array, as determined by value of
;rbx*4, to rax
cmp [a + (rbx * 4) + 4], rax ;compares next value of array with rax
jg Loop4 ;if greater, jumps to L4
xchg rax,qword[a + (rbx*4)+4] ;if less, exchanges values
mov qword[a + (rbx * 4)], rax ;and moves new value of rax to the current value of the array
Loop4:
inc rbx ;increments rbx to iterate through array
loop Loop3 ;inner loop iterates through array values once
pop rcx ;pops rcx that was previously pushed to reset count for outer loop
loop Loop2 ;loops back to L2 to iterate through all values of array again
SetWrite:
mov rbx, 0 ;resets rbx to 0
pop rcx ;pops intial count for rcx, to determine how many times
;writeint should be called
call Crlf ;prints new line
mov rdx, ExitMsg ;moves ExitMsg to rdx
call WriteString ;writes ExitMsg
WriteArray:
mov [a + rcx],rax ;moves values of array to rax, where WriteInt calls from
call WriteInt ;writes current value of rax
call Crlf ;prints new line
add rbx, 4 ;increments rbx by 4
loop WriteArray ;loops rcx times
jmp exit ;jumps to exit
WriteArrayOne:
call Crlf ;prints new line
mov rdx, ExitMsg ;moves ExitMsg to rdx
call WriteString ;writes ExitMsg
mov qword[a +rbx],rax ;moves first value of array to rax, where WriteInt calls from
call WriteInt ;writes value of rax
call Crlf ;prints new line
jmp exit ;jumps to exit
invalidInput: ;jumps here if input is not an integer
mov rdx,InputError1 ;moves InputError1 to rdx
call WriteString ;prints InputError1
jmp exit ;exits
tooManyInts:
mov rdx, InputError2 ;moves InputError2 to rdx
call WriteString ;writes InputError2
jmp exit ;exits
badInput:
cmp rcx, 1 ;if rcx == 1, prints that one value
jmp WriteArrayOne ;jumps to WriteOne which writes first int in the array
mov rdx, InputError3 ;if zero, moves InputError3 to rdx
call WriteString ;writes InputError3
jmp exit ;exits
exit: ;exits program
int 80h
Your
[a+rbx*4]
and similar all assemble to absolute addressing with 32 bit displacement. You should load the address into a register first, then apply the indexing. For example:Note that a
qword
is 8 bytes, so you should scale by that, or, if you have 4 byte integers you need to change to 32 bit register.