insert a number to a sorted array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

insert a number to a sorted array

Hi! I want to write a program with assembly (8086), to add a number into a sorted array with 10 elements. E.g my array: 2 4 9 10 12 13 16 18 30 87 I want to add 11 so my array will be: 2 4 9 10 11 12 13 16 18 30 87 I need code or Pseudocode to understand steps.

2nd Dec 2022, 7:01 PM
Saba
Saba - avatar
3 Answers
+ 1
Saba I don't have experience in asm but here's a pseudo code I guess should work... mov bx,[address of 2] mov cx,[address of 87] inc cx # increment cx mov [cx],11 # cx = addr of 87 +1, moving 11 swap_loop: #keep moving 11 left until comparing with 10 cmp [cx],[cx-1] jge finish # if 11>= 87 we're done xchg [cx],[cx-1] # otherwise swap cmp cx,bx # check to avoid underflow je finish # in case end addr = start addr dec cx # otherwise continue ... sjmp swap_loop finish: ret perhaps you want to use ax for 11 and/or other registers for indirect addressing... also can load ax with sub bx,cx before ret, so ax will contain the index which 11 has inserted...
3rd Dec 2022, 4:46 AM
Tina
Tina - avatar
+ 1
Thank U so much
3rd Dec 2022, 5:03 PM
Saba
Saba - avatar
+ 1
you're welcome Saba I dig a little more, wrote a C program to do the same thing, converted to assembly and removed noise stuff. it's almost the same steps. here's the code https://code.sololearn.com/cnafWh5vedGA/?ref=app
3rd Dec 2022, 6:50 PM
Tina
Tina - avatar