發表文章

目前顯示的是 10月, 2016的文章

ArraySum

圖片
Include Irvine32 . inc .data array  dword   11 ,   22 ,   44 ,   5 theSum  dword  ? . code main proc      mov   esi ,  offset array      mov   ecx ,  lengthof array      call  SumInt      mov  theSum ,   eax      call  writedec main endp ;------------------ SumInt proc push   esi push   ecx mov   eax ,   0 L1 :      add   eax ,   [ esi ]      add   esi ,  type  dword      loop  L1      pop   esi      pop   ecx      ret SumInt endp end main 11 + 22 + 44 + 5 = 82

MASM reverse string ( pop push )

圖片
include Irvine32 . inc .data aName  byte   "reversing a string : " ,   0dh ,   0ah ,   0 nameSize =  ( $   -  aName )   -   1                ; length of aName . code main proc mov   ecx ,  nameSize mov   esi ,   0 L1 : movzx   eax ,  aName [ esi ]                     ; put the index into eax push   eax                                  ; push into stack inc   esi                                   ; next element loop  L1 mov   esi ,   0 mov   ecx ,  nameSize L2 : pop   eax                                   ; pop out of stack mov  aName [ esi ] ,   al inc   esi loop  L2 mov   edx ,  offset aName call  writestring main endp end main before and after

copying a string

圖片
Include Irvine32 . inc .data     total  byte   "Hi, copy array " ,   0dh ,   0ah ,   0     result  byte  sizeof total dup ( 0 ) . code     main proc      mov   esi ,   0                        ; ESI = first index of array      mov   ecx ,  sizeof total            ; length of total     L1 :      mov   al ,  total [ esi ]                ; mov index in total into al every time      mov  result [ esi ] ,   al               ; store it into result      inc   esi                           ; mov to next index      Loop  L1                          ; repeat until ecx == 0      mov   edx ,  offset result           ; display result (after copying)      call  writestring     exit     main endp     end main P.159

summing an array

圖片
Include Irvine32.inc . data     total byte  "result : " ,  0dh ,  0ah ,  0     intarray dword  1 ,  2 ,  3 ,  4     sum dword  0 . code     main proc     mov esi, offset intarray         ; ESI = address of intarray     mov ecx, lengthof intarray       ; length of intarray     mov eax,  0     mov ebx,  0     L1:     add eax,  [ esi ]                    ; add an array index     add esi, type intarray           ; point to next element     add sum, eax      call  writeint      call  crlf     Loop L1                          ; repeat until ecx == 0     mov edx, offset total      call  writestring     mov ebx, sum      call  writeint     exit     main endp      end  main

JAVA copyarray

圖片
package   firstjava ; public   class   Array   {      public   static   void  main ( String [ ]  args )   {          int [ ] sourceArray  =   { 2 ,  3 ,  1 ,  5 ,  10   } ;          int [ ] targetArray  =   { 3 ,  2 ,  1 ,  2 ,  3 } ;          System . arraycopy ( sourceArray,  0 , targetArray,  0 , sourceArray. length ) ;          int  i ;          for   (  i = 0   ;  i <  sourceArray. length   ;  i ++   )              {              System . out . println ( sourceArray [ i ]   +   " copy "   +  targetArray [ i ] ) ;                          }      } } arraycopy ( sourceArray , srcPosition , targetArray , tarPosition , length)

JAVA easy random for loop

圖片
package   firstjava ; import   java.util.Scanner ; public   class  Forloop  {      public   static   void  main ( String [ ]  args )   {     java. util . Scanner  input  =   new  java. util . Scanner ( System . in ) ;      int  i ;      int [ ]  mylist  =   new   int [ 10 ] ;      for   (  i = 0   ;  i < mylist. length   ;  i ++   ) {         mylist [ i ]   =   ( int ) ( Math . random ( ) * 100 ) ;          System . out . println (  mylist [ i ]   +   " "   ) ;      }      } } mistake you may make:       on line 10.     ( int )  Math . random ( ) * 100 先做前面double變成0 output: